Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

embed version in binary

Hayden0b92d57b85ba
diff --git a/Dockerfile b/Dockerfile
index 08b4ecfc..fa6e5fd3 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -17,7 +17,10 @@ COPY ./backend .
RUN go get -d -v ./...
RUN rm -rf ./app/api/public
COPY --from=frontend-builder /app/.output/public ./app/api/public
-RUN CGO_ENABLED=1 GOOS=linux go build -o /go/bin/api -v ./app/api/*.go
+RUN CGO_ENABLED=1 GOOS=linux go build \
+ -o /go/bin/api \
+ -v ./app/api/*.go \
+ -ldflags "-s -w -X main.Commit `git rev-parse HEAD` -X main.BuildTime `date -u +%Y-%m-%dT%H:%M:%SZ`"
# Production Stage
FROM alpine:latest
diff --git a/backend/app/api/docs/docs.go b/backend/app/api/docs/docs.go
index b0b3d517..fe8b893f 100644
--- a/backend/app/api/docs/docs.go
+++ b/backend/app/api/docs/docs.go
@@ -821,6 +821,9 @@ const docTemplate = `{
"types.ApiSummary": {
"type": "object",
"properties": {
+ "build": {
+ "$ref": "#/definitions/types.Build"
+ },
"health": {
"type": "boolean"
},
@@ -838,6 +841,20 @@ const docTemplate = `{
}
}
},
+ "types.Build": {
+ "type": "object",
+ "properties": {
+ "build_time": {
+ "type": "string"
+ },
+ "commit": {
+ "type": "string"
+ },
+ "version": {
+ "type": "string"
+ }
+ }
+ },
"types.DocumentOut": {
"type": "object",
"properties": {
diff --git a/backend/app/api/docs/swagger.json b/backend/app/api/docs/swagger.json
index f69bd9cf..06f67eda 100644
--- a/backend/app/api/docs/swagger.json
+++ b/backend/app/api/docs/swagger.json
@@ -813,6 +813,9 @@
"types.ApiSummary": {
"type": "object",
"properties": {
+ "build": {
+ "$ref": "#/definitions/types.Build"
+ },
"health": {
"type": "boolean"
},
@@ -830,6 +833,20 @@
}
}
},
+ "types.Build": {
+ "type": "object",
+ "properties": {
+ "build_time": {
+ "type": "string"
+ },
+ "commit": {
+ "type": "string"
+ },
+ "version": {
+ "type": "string"
+ }
+ }
+ },
"types.DocumentOut": {
"type": "object",
"properties": {
diff --git a/backend/app/api/docs/swagger.yaml b/backend/app/api/docs/swagger.yaml
index f75f3657..5d52cffb 100644
--- a/backend/app/api/docs/swagger.yaml
+++ b/backend/app/api/docs/swagger.yaml
@@ -16,6 +16,8 @@ definitions:
type: object
types.ApiSummary:
properties:
+ build:
+ $ref: '#/definitions/types.Build'
health:
type: boolean
message:
@@ -27,6 +29,15 @@ definitions:
type: string
type: array
type: object
+ types.Build:
+ properties:
+ build_time:
+ type: string
+ commit:
+ type: string
+ version:
+ type: string
+ type: object
types.DocumentOut:
properties:
id:
diff --git a/backend/app/api/main.go b/backend/app/api/main.go
index d06faa0e..f4a8d1aa 100644
--- a/backend/app/api/main.go
+++ b/backend/app/api/main.go
@@ -14,6 +14,12 @@ import (
"github.com/rs/zerolog/log"
)
+var (
+ Version = "0.1.0"
+ Commit = "HEAD"
+ BuildTime = "now"
+)
+
// @title Go API Templates
// @version 1.0
// @description This is a simple Rest API Server Template that implements some basic User and Authentication patterns to help you get started and bootstrap your next project!.
diff --git a/backend/app/api/routes.go b/backend/app/api/routes.go
index 1ee0a74a..a82a3835 100644
--- a/backend/app/api/routes.go
+++ b/backend/app/api/routes.go
@@ -14,6 +14,7 @@ import (
_ "github.com/hay-kot/content/backend/app/api/docs"
v1 "github.com/hay-kot/content/backend/app/api/v1"
"github.com/hay-kot/content/backend/internal/repo"
+ "github.com/hay-kot/content/backend/internal/types"
"github.com/rs/zerolog/log"
httpSwagger "github.com/swaggo/http-swagger" // http-swagger middleware
)
@@ -44,7 +45,11 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux {
v1Base := v1.BaseUrlFunc(prefix)
v1Ctrl := v1.NewControllerV1(a.services)
{
- r.Get(v1Base("/status"), v1Ctrl.HandleBase(func() bool { return true }, "v1"))
+ r.Get(v1Base("/status"), v1Ctrl.HandleBase(func() bool { return true }, types.Build{
+ Version: Version,
+ Commit: Commit,
+ BuildTime: BuildTime,
+ }))
r.Post(v1Base("/users/register"), v1Ctrl.HandleUserRegistration())
r.Post(v1Base("/users/login"), v1Ctrl.HandleAuthLogin())
diff --git a/backend/app/api/v1/controller.go b/backend/app/api/v1/controller.go
index 3ba0f7b2..dda2e208 100644
--- a/backend/app/api/v1/controller.go
+++ b/backend/app/api/v1/controller.go
@@ -37,13 +37,13 @@ type ReadyFunc func() bool
// @Produce json
// @Success 200 {object} types.ApiSummary
// @Router /v1/status [GET]
-func (ctrl *V1Controller) HandleBase(ready ReadyFunc, versions ...string) http.HandlerFunc {
+func (ctrl *V1Controller) HandleBase(ready ReadyFunc, build types.Build) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
server.Respond(w, http.StatusOK, types.ApiSummary{
- Healthy: ready(),
- Versions: versions,
- Title: "Go API Template",
- Message: "Welcome to the Go API Template Application!",
+ Healthy: ready(),
+ Title: "Go API Template",
+ Message: "Welcome to the Go API Template Application!",
+ Build: build,
})
}
}
diff --git a/backend/app/api/v1/controller_test.go b/backend/app/api/v1/controller_test.go
index b2f87cd3..11e008ce 100644
--- a/backend/app/api/v1/controller_test.go
+++ b/backend/app/api/v1/controller_test.go
@@ -5,6 +5,7 @@ import (
"net/http/httptest"
"testing"
+ "github.com/hay-kot/content/backend/internal/types"
"github.com/stretchr/testify/assert"
)
@@ -21,7 +22,11 @@ func Test_NewHandlerV1(t *testing.T) {
func TestHandlersv1_HandleBase(t *testing.T) {
// Setup
- hdlrFunc := mockHandler.HandleBase(func() bool { return true }, "v1")
+ hdlrFunc := mockHandler.HandleBase(func() bool { return true }, types.Build{
+ Version: "0.1.0",
+ Commit: "HEAD",
+ BuildTime: "now",
+ })
// Call Handler Func
rr := httptest.NewRecorder()
@@ -33,7 +38,7 @@ func TestHandlersv1_HandleBase(t *testing.T) {
}
// Validate Json Payload
- expected := `{"health":true,"versions":["v1"],"title":"Go API Template","message":"Welcome to the Go API Template Application!"}`
+ expected := `{"health":true,"versions":null,"title":"Go API Template","message":"Welcome to the Go API Template Application!","Build":{"version":"0.1.0","commit":"HEAD","build_time":"now"}}`
if rr.Body.String() != expected {
t.Errorf("Expected json to be %s, got %s", expected, rr.Body.String())
diff --git a/backend/internal/types/about_types.go b/backend/internal/types/about_types.go
index 7db4bd9e..76c60bc2 100644
--- a/backend/internal/types/about_types.go
+++ b/backend/internal/types/about_types.go
@@ -8,4 +8,11 @@ type ApiSummary struct {
Versions []string `json:"versions"`
Title string `json:"title"`
Message string `json:"message"`
+ Build Build
+}
+
+type Build struct {
+ Version string `json:"version"`
+ Commit string `json:"commit"`
+ BuildTime string `json:"build_time"`
}
diff --git a/frontend/lib/api/types/data-contracts.ts b/frontend/lib/api/types/data-contracts.ts
index fa488aa2..a802309e 100644
--- a/frontend/lib/api/types/data-contracts.ts
+++ b/frontend/lib/api/types/data-contracts.ts
@@ -22,12 +22,19 @@ export interface ServerResults {
}
export interface ApiSummary {
+ build: Build;
health: boolean;
message: string;
title: string;
versions: string[];
}
+export interface Build {
+ build_time: string;
+ commit: string;
+ version: string;
+}
+
export interface DocumentOut {
id: string;
path: string;