embed version in binary

This commit is contained in:
Hayden 2022-09-13 20:06:07 -08:00
parent ca36e3b080
commit 0b92d57b85
10 changed files with 87 additions and 9 deletions

View file

@ -17,7 +17,10 @@ COPY ./backend .
RUN go get -d -v ./... RUN go get -d -v ./...
RUN rm -rf ./app/api/public RUN rm -rf ./app/api/public
COPY --from=frontend-builder /app/.output/public ./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 # Production Stage
FROM alpine:latest FROM alpine:latest

View file

@ -821,6 +821,9 @@ const docTemplate = `{
"types.ApiSummary": { "types.ApiSummary": {
"type": "object", "type": "object",
"properties": { "properties": {
"build": {
"$ref": "#/definitions/types.Build"
},
"health": { "health": {
"type": "boolean" "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": { "types.DocumentOut": {
"type": "object", "type": "object",
"properties": { "properties": {

View file

@ -813,6 +813,9 @@
"types.ApiSummary": { "types.ApiSummary": {
"type": "object", "type": "object",
"properties": { "properties": {
"build": {
"$ref": "#/definitions/types.Build"
},
"health": { "health": {
"type": "boolean" "type": "boolean"
}, },
@ -830,6 +833,20 @@
} }
} }
}, },
"types.Build": {
"type": "object",
"properties": {
"build_time": {
"type": "string"
},
"commit": {
"type": "string"
},
"version": {
"type": "string"
}
}
},
"types.DocumentOut": { "types.DocumentOut": {
"type": "object", "type": "object",
"properties": { "properties": {

View file

@ -16,6 +16,8 @@ definitions:
type: object type: object
types.ApiSummary: types.ApiSummary:
properties: properties:
build:
$ref: '#/definitions/types.Build'
health: health:
type: boolean type: boolean
message: message:
@ -27,6 +29,15 @@ definitions:
type: string type: string
type: array type: array
type: object type: object
types.Build:
properties:
build_time:
type: string
commit:
type: string
version:
type: string
type: object
types.DocumentOut: types.DocumentOut:
properties: properties:
id: id:

View file

@ -14,6 +14,12 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
var (
Version = "0.1.0"
Commit = "HEAD"
BuildTime = "now"
)
// @title Go API Templates // @title Go API Templates
// @version 1.0 // @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!. // @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!.

View file

@ -14,6 +14,7 @@ import (
_ "github.com/hay-kot/content/backend/app/api/docs" _ "github.com/hay-kot/content/backend/app/api/docs"
v1 "github.com/hay-kot/content/backend/app/api/v1" 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/repo"
"github.com/hay-kot/content/backend/internal/types"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
httpSwagger "github.com/swaggo/http-swagger" // http-swagger middleware 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) v1Base := v1.BaseUrlFunc(prefix)
v1Ctrl := v1.NewControllerV1(a.services) 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/register"), v1Ctrl.HandleUserRegistration())
r.Post(v1Base("/users/login"), v1Ctrl.HandleAuthLogin()) r.Post(v1Base("/users/login"), v1Ctrl.HandleAuthLogin())

View file

@ -37,13 +37,13 @@ type ReadyFunc func() bool
// @Produce json // @Produce json
// @Success 200 {object} types.ApiSummary // @Success 200 {object} types.ApiSummary
// @Router /v1/status [GET] // @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) { return func(w http.ResponseWriter, r *http.Request) {
server.Respond(w, http.StatusOK, types.ApiSummary{ server.Respond(w, http.StatusOK, types.ApiSummary{
Healthy: ready(), Healthy: ready(),
Versions: versions,
Title: "Go API Template", Title: "Go API Template",
Message: "Welcome to the Go API Template Application!", Message: "Welcome to the Go API Template Application!",
Build: build,
}) })
} }
} }

View file

@ -5,6 +5,7 @@ import (
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/hay-kot/content/backend/internal/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -21,7 +22,11 @@ func Test_NewHandlerV1(t *testing.T) {
func TestHandlersv1_HandleBase(t *testing.T) { func TestHandlersv1_HandleBase(t *testing.T) {
// Setup // 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 // Call Handler Func
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
@ -33,7 +38,7 @@ func TestHandlersv1_HandleBase(t *testing.T) {
} }
// Validate Json Payload // 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 { if rr.Body.String() != expected {
t.Errorf("Expected json to be %s, got %s", expected, rr.Body.String()) t.Errorf("Expected json to be %s, got %s", expected, rr.Body.String())

View file

@ -8,4 +8,11 @@ type ApiSummary struct {
Versions []string `json:"versions"` Versions []string `json:"versions"`
Title string `json:"title"` Title string `json:"title"`
Message string `json:"message"` Message string `json:"message"`
Build Build
}
type Build struct {
Version string `json:"version"`
Commit string `json:"commit"`
BuildTime string `json:"build_time"`
} }

View file

@ -22,12 +22,19 @@ export interface ServerResults {
} }
export interface ApiSummary { export interface ApiSummary {
build: Build;
health: boolean; health: boolean;
message: string; message: string;
title: string; title: string;
versions: string[]; versions: string[];
} }
export interface Build {
build_time: string;
commit: string;
version: string;
}
export interface DocumentOut { export interface DocumentOut {
id: string; id: string;
path: string; path: string;