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/% 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": diff --git a/internal/pkg/api/api.go b/internal/pkg/api/api.go index 94c125b..761b33d 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, @@ -50,12 +37,13 @@ func New(config *config.Config) http.Handler { logger := httplog.NewLogger("bws-cache", httplog.Options{ JSON: true, LogLevel: slog.LevelInfo, - Concise: true, + Concise: false, RequestHeaders: true, - MessageFieldName: "message", + MessageFieldName: "msg", TimeFieldFormat: time.RFC3339, + TimeFieldName: "time", Tags: map[string]string{ - "version": commit, + "version": c.Commit, }, QuietDownRoutes: []string{ "/", 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 diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index ea3698d..347a1dd 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -1,6 +1,7 @@ package config import ( + _ "embed" "strings" "time" @@ -19,6 +20,10 @@ type Config struct { Connection client.Bitwarden } +//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() v.SetEnvPrefix("bws_cache")