From 4e406c95dc7e5b07c38d3bce0c0081c1939ca702 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Mon, 2 Sep 2024 09:24:11 -0400 Subject: [PATCH 1/7] Add commit function as part of config so can be used elsewhere --- internal/pkg/config/config.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index ea3698d..6dba773 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -1,6 +1,7 @@ package config import ( + "runtime/debug" "strings" "time" @@ -19,6 +20,18 @@ type Config struct { Connection client.Bitwarden } +var Commit = func() string { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } + } + } + + return "" +}() + func LoadConfig(config *Config) { v := viper.New() v.SetEnvPrefix("bws_cache") From 280efc57c10c5189116d1c0cd00332df8ef8c4d7 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Mon, 2 Sep 2024 09:24:53 -0400 Subject: [PATCH 2/7] Dynamic verison, allow anycase loglevels --- cmd/bws-cache/bws-cache.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/bws-cache/bws-cache.go b/cmd/bws-cache/bws-cache.go index 81c204c..47b46d3 100644 --- a/cmd/bws-cache/bws-cache.go +++ b/cmd/bws-cache/bws-cache.go @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" "os" + "strings" c "bws-cache/internal/pkg/config" h "bws-cache/internal/pkg/http" @@ -51,7 +52,7 @@ func main() { } func version() { - fmt.Println("0.1.0") + fmt.Println(c.Commit) } func start() { @@ -89,7 +90,7 @@ func start() { } func getLoggerLevel(config string) slog.Level { - switch config { + switch strings.ToUpper(config) { case "DEBUG": return slog.LevelDebug case "WARN": From cc050c9f07c4417b28e758278a68a5fda0a09726 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Mon, 2 Sep 2024 09:25:17 -0400 Subject: [PATCH 3/7] log formatting/use config.Commit --- internal/pkg/api/api.go | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/internal/pkg/api/api.go b/internal/pkg/api/api.go index 94c125b..ef02461 100644 --- a/internal/pkg/api/api.go +++ b/internal/pkg/api/api.go @@ -4,12 +4,11 @@ import ( "fmt" "log/slog" "net/http" - "runtime/debug" "strings" "time" "bws-cache/internal/pkg/client" - "bws-cache/internal/pkg/config" + c "bws-cache/internal/pkg/config" "bws-cache/internal/pkg/metrics" "github.com/go-chi/chi/v5" @@ -19,18 +18,6 @@ import ( "github.com/pkg/errors" ) -var commit = func() string { - if info, ok := debug.ReadBuildInfo(); ok { - for _, setting := range info.Settings { - if setting.Key == "vcs.revision" { - return setting.Value - } - } - } - - return "" -}() - type API struct { SecretTTL time.Duration WebTTL time.Duration @@ -39,7 +26,7 @@ type API struct { Metrics *metrics.BwsMetrics } -func New(config *config.Config) http.Handler { +func New(config *c.Config) http.Handler { api := API{ SecretTTL: config.SecretTTL, OrgID: config.OrgID, @@ -52,10 +39,11 @@ func New(config *config.Config) http.Handler { LogLevel: slog.LevelInfo, Concise: true, RequestHeaders: true, - MessageFieldName: "message", + MessageFieldName: "msg", TimeFieldFormat: time.RFC3339, + TimeFieldName: "time", Tags: map[string]string{ - "version": commit, + "version": c.Commit, }, QuietDownRoutes: []string{ "/", From 66b34e206d9f0b4fcccca232e39e791e8f4a1555 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Mon, 2 Sep 2024 09:39:28 -0400 Subject: [PATCH 4/7] concise logging seems to remove tags --- internal/pkg/api/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pkg/api/api.go b/internal/pkg/api/api.go index ef02461..761b33d 100644 --- a/internal/pkg/api/api.go +++ b/internal/pkg/api/api.go @@ -37,7 +37,7 @@ func New(config *c.Config) http.Handler { logger := httplog.NewLogger("bws-cache", httplog.Options{ JSON: true, LogLevel: slog.LevelInfo, - Concise: true, + Concise: false, RequestHeaders: true, MessageFieldName: "msg", TimeFieldFormat: time.RFC3339, From dc6b58513614cafd5d9415165896b90e16ac4fac Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Mon, 2 Sep 2024 15:16:19 -0400 Subject: [PATCH 5/7] git needs to be installed --- Dockerfile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6f28745..6b8408c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,10 @@ FROM golang:alpine AS builder -RUN apk add --no-cache musl-dev gcc +RUN apk add --no-cache musl-dev gcc git COPY . . -RUN cd cmd/bws-cache && go build -ldflags='-s -w' -trimpath -o /dist/bws-cache +RUN go generate ./internal/pkg/config/config.go +RUN cd cmd/bws-cache && go build -ldflags='-s -w' -o /dist/bws-cache RUN ldd /dist/bws-cache | tr -s [:blank:] '\n' | grep ^/ | xargs -I % install -D % /dist/% From bf60fce465bc4799aa42f1bc673158d16575387d Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Mon, 2 Sep 2024 15:16:53 -0400 Subject: [PATCH 6/7] placeholder version --- internal/pkg/config/commit.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 internal/pkg/config/commit.txt diff --git a/internal/pkg/config/commit.txt b/internal/pkg/config/commit.txt new file mode 100644 index 0000000..b3a4252 --- /dev/null +++ b/internal/pkg/config/commit.txt @@ -0,0 +1 @@ +placeholder \ No newline at end of file From c1e71266176e7f68f75c95121faf9f9630269fb6 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Mon, 2 Sep 2024 15:21:11 -0400 Subject: [PATCH 7/7] embedd commit --- internal/pkg/config/config.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 6dba773..347a1dd 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -1,7 +1,7 @@ package config import ( - "runtime/debug" + _ "embed" "strings" "time" @@ -20,17 +20,9 @@ type Config struct { Connection client.Bitwarden } -var Commit = func() string { - if info, ok := debug.ReadBuildInfo(); ok { - for _, setting := range info.Settings { - if setting.Key == "vcs.revision" { - return setting.Value - } - } - } - - return "" -}() +//go:generate sh -c "printf %s $(git rev-parse HEAD) > commit.txt" +//go:embed commit.txt +var Commit string func LoadConfig(config *Config) { v := viper.New()