diff --git a/cmd/server/main.go b/cmd/server/main.go index accaf6a..7bbcc0a 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -19,7 +19,7 @@ import ( var version = "dev" func main() { - cfg := config.Load() + cfg := config.Load(version) a, err := app.New(cfg) if err != nil { diff --git a/internal/config/config.go b/internal/config/config.go index 6c31fbe..72aa659 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -33,9 +33,11 @@ type Config struct { MailerEmailFrom string SupportEmail string + + Version string } -func Load() *Config { +func Load(version string) *Config { if err := godotenv.Load(); err != nil { slog.Info("no .env file found, using environment variables") @@ -65,6 +67,8 @@ func Load() *Config { MailerEmailFrom: envString("MAILER_EMAIL_FROM", ""), SupportEmail: envString("SUPPORT_EMAIL", ""), + + Version: version, } return cfg @@ -87,6 +91,8 @@ func (c *Config) Sanitized() *Config { MailerEmailFrom: c.MailerEmailFrom, SupportEmail: c.SupportEmail, + + Version: c.Version, } } diff --git a/internal/ctxkeys/ctx.go b/internal/ctxkeys/ctx.go index aa56624..a4d5e4c 100644 --- a/internal/ctxkeys/ctx.go +++ b/internal/ctxkeys/ctx.go @@ -8,11 +8,12 @@ import ( ) const ( - UserKey string = "user" - ProfileKey string = "profile" - URLPathKey string = "url_path" - ConfigKey string = "config" - CSRFTokenKey string = "csrf_token" + UserKey string = "user" + ProfileKey string = "profile" + URLPathKey string = "url_path" + ConfigKey string = "config" + CSRFTokenKey string = "csrf_token" + AppVersionKey string = "app_version" ) func User(ctx context.Context) *model.User { @@ -59,3 +60,12 @@ func CSRFToken(ctx context.Context) string { func WithCSRFToken(ctx context.Context, token string) context.Context { return context.WithValue(ctx, CSRFTokenKey, token) } + +func AppVersion(ctx context.Context) string { + version, _ := ctx.Value(AppVersionKey).(string) + return version +} + +func WithAppVersion(ctx context.Context, version string) context.Context { + return context.WithValue(ctx, AppVersionKey, version) +} diff --git a/internal/middleware/app_version.go b/internal/middleware/app_version.go new file mode 100644 index 0000000..ad9718a --- /dev/null +++ b/internal/middleware/app_version.go @@ -0,0 +1,16 @@ +package middleware + +import ( + "net/http" + + "git.juancwu.dev/juancwu/budgit/internal/ctxkeys" +) + +func AppVersion(version string) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := ctxkeys.WithAppVersion(r.Context(), version) + next.ServeHTTP(w, r.WithContext(ctx)) + }) + } +} diff --git a/internal/routes/routes.go b/internal/routes/routes.go index b63ba9e..4a70050 100644 --- a/internal/routes/routes.go +++ b/internal/routes/routes.go @@ -181,6 +181,7 @@ func SetupRoutes(a *app.App) http.Handler { // Global middlewares handler := middleware.Chain( mux, + middleware.AppVersion(a.Cfg.Version), middleware.Config(a.Cfg), middleware.RequestLogging, middleware.NoCacheDynamic, diff --git a/internal/ui/layouts/app.templ b/internal/ui/layouts/app.templ index 416c54f..1da5be7 100644 --- a/internal/ui/layouts/app.templ +++ b/internal/ui/layouts/app.templ @@ -68,6 +68,9 @@ templ App(title string) { } } } +