Skip to content

Commit

Permalink
feat: add build info
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed Apr 11, 2022
1 parent 7d9722c commit dc1757e
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 54 deletions.
2 changes: 2 additions & 0 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bootstrap
import (
"context"

"github.com/JulesMike/spoty/build"
"github.com/JulesMike/spoty/cache"
"github.com/JulesMike/spoty/config"
"github.com/JulesMike/spoty/http"
Expand All @@ -12,6 +13,7 @@ import (

// Module exported for initialising application.
var Module = fx.Options(
build.Module,
config.Module,
cache.Module,
http.Module,
Expand Down
56 changes: 56 additions & 0 deletions build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package build

import (
"fmt"
"log"
"runtime/debug"
"time"

"go.uber.org/fx"
)

// Module exported for initialising a new build Info.
var Module = fx.Options(
fx.Provide(New),
)

// Info contains the information about the build.
type Info struct {
Revision string `json:"revision"`
LastCommit time.Time `json:"last_commit"`
DirtyBuild bool `json:"dirty_build"`
}

// New returns a new instance of Info.
func New() (*Info, error) {
bi, ok := debug.ReadBuildInfo()
if !ok {
return nil, fmt.Errorf("failed to read build info")
}

info := Info{
Revision: "n/a",
LastCommit: time.Time{},
DirtyBuild: false,
}

for i := range bi.Settings {
kv := &bi.Settings[i]

switch kv.Key {
case "vcs.revision":
info.Revision = kv.Value
case "vcs.time":
hash, err := time.Parse(time.RFC3339, kv.Value)
if err != nil {
log.Printf("failed to parse vcs.time: %v", err)
}

info.LastCommit = hash
case "vcs.modified":
info.DirtyBuild = kv.Value == "true"
}
}

return &info, nil
}
60 changes: 47 additions & 13 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/api": {
"/": {
"get": {
"description": "checks if server is running",
"produces": [
Expand All @@ -36,7 +36,7 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/server.Success"
"$ref": "#/definitions/http.Success"
}
}
}
Expand All @@ -62,7 +62,7 @@ const docTemplate = `{
"403": {
"description": "already authenticated",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
}
}
Expand Down Expand Up @@ -98,19 +98,19 @@ const docTemplate = `{
"200": {
"description": "authenticated successfully",
"schema": {
"$ref": "#/definitions/server.Success"
"$ref": "#/definitions/http.Success"
}
},
"403": {
"description": "could not retrieve token",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
},
"404": {
"description": "could not retrieve current user",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
}
}
Expand All @@ -136,13 +136,13 @@ const docTemplate = `{
"401": {
"description": "not authenticated",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
},
"404": {
"description": "no current playing track found",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
}
}
Expand Down Expand Up @@ -171,26 +171,60 @@ const docTemplate = `{
"401": {
"description": "not authenticated",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
},
"404": {
"description": "no current playing track found",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
},
"500": {
"description": "album images could not be processed",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
}
}
}
},
"/api/version": {
"get": {
"description": "checks the server's version",
"produces": [
"application/json"
],
"tags": [
"core"
],
"summary": "Health Check",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/build.Info"
}
}
}
}
}
},
"definitions": {
"build.Info": {
"type": "object",
"properties": {
"dirty_build": {
"type": "boolean"
},
"last_commit": {
"type": "string"
},
"revision": {
"type": "string"
}
}
},
"color.RGBA": {
"type": "object",
"properties": {
Expand All @@ -199,15 +233,15 @@ const docTemplate = `{
}
}
},
"server.Error": {
"http.Error": {
"type": "object",
"properties": {
"error": {
"type": "string"
}
}
},
"server.Success": {
"http.Success": {
"type": "object",
"properties": {
"success": {
Expand Down
60 changes: 47 additions & 13 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": "v0.2.0"
},
"paths": {
"/api": {
"/": {
"get": {
"description": "checks if server is running",
"produces": [
Expand All @@ -27,7 +27,7 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/server.Success"
"$ref": "#/definitions/http.Success"
}
}
}
Expand All @@ -53,7 +53,7 @@
"403": {
"description": "already authenticated",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
}
}
Expand Down Expand Up @@ -89,19 +89,19 @@
"200": {
"description": "authenticated successfully",
"schema": {
"$ref": "#/definitions/server.Success"
"$ref": "#/definitions/http.Success"
}
},
"403": {
"description": "could not retrieve token",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
},
"404": {
"description": "could not retrieve current user",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
}
}
Expand All @@ -127,13 +127,13 @@
"401": {
"description": "not authenticated",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
},
"404": {
"description": "no current playing track found",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
}
}
Expand Down Expand Up @@ -162,26 +162,60 @@
"401": {
"description": "not authenticated",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
},
"404": {
"description": "no current playing track found",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
},
"500": {
"description": "album images could not be processed",
"schema": {
"$ref": "#/definitions/server.Error"
"$ref": "#/definitions/http.Error"
}
}
}
}
},
"/api/version": {
"get": {
"description": "checks the server's version",
"produces": [
"application/json"
],
"tags": [
"core"
],
"summary": "Health Check",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/build.Info"
}
}
}
}
}
},
"definitions": {
"build.Info": {
"type": "object",
"properties": {
"dirty_build": {
"type": "boolean"
},
"last_commit": {
"type": "string"
},
"revision": {
"type": "string"
}
}
},
"color.RGBA": {
"type": "object",
"properties": {
Expand All @@ -190,15 +224,15 @@
}
}
},
"server.Error": {
"http.Error": {
"type": "object",
"properties": {
"error": {
"type": "string"
}
}
},
"server.Success": {
"http.Success": {
"type": "object",
"properties": {
"success": {
Expand Down
Loading

0 comments on commit dc1757e

Please sign in to comment.