From 0b92d57b85baa01d373d22ff195e0463358f2048 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Tue, 13 Sep 2022 20:06:07 -0800 Subject: [PATCH] embed version in binary --- Dockerfile | 5 ++++- backend/app/api/docs/docs.go | 17 +++++++++++++++++ backend/app/api/docs/swagger.json | 17 +++++++++++++++++ backend/app/api/docs/swagger.yaml | 11 +++++++++++ backend/app/api/main.go | 6 ++++++ backend/app/api/routes.go | 7 ++++++- backend/app/api/v1/controller.go | 10 +++++----- backend/app/api/v1/controller_test.go | 9 +++++++-- backend/internal/types/about_types.go | 7 +++++++ frontend/lib/api/types/data-contracts.ts | 7 +++++++ 10 files changed, 87 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 08b4ecf..fa6e5fd 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 b0b3d51..fe8b893 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 f69bd9c..06f67ed 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 f75f365..5d52cff 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 d06faa0..f4a8d1a 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 1ee0a74..a82a383 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 3ba0f7b..dda2e20 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 b2f87cd..11e008c 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 7db4bd9..76c60bc 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 fa488aa..a802309 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;