From 0d530e597a1d54c29153077597bc3fb701e68a38 Mon Sep 17 00:00:00 2001 From: vanadium23 Date: Mon, 24 Feb 2025 22:25:14 +0300 Subject: [PATCH 1/2] web: add link & version to footer closes #11 --- docker-compose-integration.yml | 7 +++++++ integration-test/integration_test.go | 10 ++++++++++ internal/controller/http/web/router.go | 4 ++++ web/templates/layouts/master.html | 2 +- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docker-compose-integration.yml b/docker-compose-integration.yml index 8f65afd..84f5552 100644 --- a/docker-compose-integration.yml +++ b/docker-compose-integration.yml @@ -5,6 +5,13 @@ services: tmpfs: - /pgtmpfs + app: + build: + context: . + dockerfile: Dockerfile + args: + - KOMPANION_VERSION=integration + integration: build: context: . diff --git a/integration-test/integration_test.go b/integration-test/integration_test.go index 5dba4b9..70a137f 100644 --- a/integration-test/integration_test.go +++ b/integration-test/integration_test.go @@ -59,6 +59,16 @@ func healthCheck(attempts int) error { return err } +func TestWebFooterVersion(t *testing.T) { + Test(t, + Description("Footer Version"), + Get(basePath+"/"), + Expect().Status().Equal(http.StatusOK), + Expect().Body().String().Contains("github.com/vanadium23/kompanion"), + Expect().Body().String().Contains("integration"), + ) +} + // New tests based on controller/web // login/page func TestWebAuthUser(t *testing.T) { diff --git a/internal/controller/http/web/router.go b/internal/controller/http/web/router.go index 0c07570..a6e2ba7 100644 --- a/internal/controller/http/web/router.go +++ b/internal/controller/http/web/router.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "html/template" + "os" "time" "github.com/foolin/goview" @@ -66,6 +67,9 @@ func NewRouter( "LoadTimes": func(startTime time.Time) string { return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms" }, + "Version": func() string { + return os.Getenv("KOMPANION_VERSION") + }, } handler.HTMLRender = ginview.New(config) diff --git a/web/templates/layouts/master.html b/web/templates/layouts/master.html index a5bc182..f5a280c 100644 --- a/web/templates/layouts/master.html +++ b/web/templates/layouts/master.html @@ -38,7 +38,7 @@

- Powered by KOmpanion, Version: TDB, Page: {{ LoadTimes .startTime }} + Powered by KOmpanion, Version: {{ Version }}, Page: {{ LoadTimes .startTime }}

From 6f588fe48e3abf9aff0cc8f65440ecd7e23b3112 Mon Sep 17 00:00:00 2001 From: vanadium23 Date: Thu, 27 Feb 2025 22:42:40 +0300 Subject: [PATCH 2/2] cmd: add version inline compile closes #11 --- Dockerfile | 9 +++++---- cmd/app/main.go | 4 +++- config/config.go | 19 +++++-------------- internal/app/app.go | 2 +- internal/controller/http/web/router.go | 4 ++-- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index 37b468b..b1fc0ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,16 +10,17 @@ RUN apk add --update gcc musl-dev COPY --from=modules /go/pkg /go/pkg COPY . /app WORKDIR /app + +ARG KOMPANION_VERSION=local +ENV KOMPANION_VERSION=$KOMPANION_VERSION + RUN GOOS=linux GOARCH=amd64 \ - go build -tags migrate -o /bin/app ./cmd/app + go build -ldflags "-X main.Version=$KOMPANION_VERSION" -tags migrate -o /bin/app ./cmd/app # Step 3: Final FROM golang:1.22.5-alpine ENV GIN_MODE=release -ARG KOMPANION_VERSION=local -ENV KOMPANION_VERSION=$KOMPANION_VERSION - WORKDIR / COPY --from=builder /app/config /config COPY --from=builder /app/migrations /migrations diff --git a/cmd/app/main.go b/cmd/app/main.go index dde959a..0e279b9 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -7,9 +7,11 @@ import ( "github.com/vanadium23/kompanion/internal/app" ) +var Version = "dev" + func main() { // Configuration - cfg, err := config.NewConfig() + cfg, err := config.NewConfig(Version) if err != nil { log.Fatalf("Config error: %s", err) } diff --git a/config/config.go b/config/config.go index 2456854..396e119 100644 --- a/config/config.go +++ b/config/config.go @@ -61,12 +61,7 @@ type ( ) // NewConfig - reads from env, validates and returns the config. -func NewConfig() (*Config, error) { - app, err := readAppConfig() - if err != nil { - return nil, err - } - +func NewConfig(version string) (*Config, error) { auth, err := readAuthConfig() if err != nil { return nil, err @@ -98,7 +93,10 @@ func NewConfig() (*Config, error) { } return &Config{ - App: app, + App: App{ + Name: "kompanion", + Version: version, + }, Auth: auth, HTTP: http, Log: log, @@ -108,13 +106,6 @@ func NewConfig() (*Config, error) { }, nil } -func readAppConfig() (App, error) { - return App{ - Name: "kompanion", - Version: "0.0.1", - }, nil -} - func readAuthConfig() (Auth, error) { username := readPrefixedEnv("AUTH_USERNAME") password := readPrefixedEnv("AUTH_PASSWORD") diff --git a/internal/app/app.go b/internal/app/app.go index b04c59f..dfca835 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -65,7 +65,7 @@ func Run(cfg *config.Config) { // HTTP Server handler := gin.New() - web.NewRouter(handler, l, authService, progress, shelf, rs) + web.NewRouter(handler, l, authService, progress, shelf, rs, cfg.Version) v1.NewRouter(handler, l, authService, progress, shelf) opds.NewRouter(handler, l, authService, progress, shelf) webdav.NewRouter(handler, authService, l, rs, cfg.StatsStorage.Path) diff --git a/internal/controller/http/web/router.go b/internal/controller/http/web/router.go index a6e2ba7..2871ae3 100644 --- a/internal/controller/http/web/router.go +++ b/internal/controller/http/web/router.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "html/template" - "os" "time" "github.com/foolin/goview" @@ -38,6 +37,7 @@ func NewRouter( p sync.Progress, shelf library.Shelf, stats stats.ReadingStats, + version string, ) { // Options handler.Use(gin.Logger()) @@ -68,7 +68,7 @@ func NewRouter( return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms" }, "Version": func() string { - return os.Getenv("KOMPANION_VERSION") + return template.HTMLEscapeString(version) }, } handler.HTMLRender = ginview.New(config)