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 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

View File

@ -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": {

View File

@ -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": {

View File

@ -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:

View File

@ -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!.

View File

@ -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())

View File

@ -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,
})
}
}

View File

@ -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())

View File

@ -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"`
}

View File

@ -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;