diff --git a/flake.nix b/flake.nix index 5dd141d..d9769ae 100644 --- a/flake.nix +++ b/flake.nix @@ -81,7 +81,6 @@ templ tailwindcss makeWrapper - nix ]; preBuild = let @@ -95,7 +94,7 @@ inherit (builtins) length genList elemAt; in '' - ldflags+=" -X main.hash=$(nix-hash ${./.})" + ldflags+=" -X main.rev=${if lib.hasAttr "rev" self then self.rev else self.dirtyRev}" ${builtins.concatStringsSep "\n" ( genList ( @@ -144,7 +143,7 @@ name = "buildapp"; text = '' ${packages.default.preBuild} - go build + go build -ldflags "$ldflags" -o app ./app ''; }) diff --git a/main.go b/main.go index dc414e4..b036349 100644 --- a/main.go +++ b/main.go @@ -7,14 +7,17 @@ import ( "app/pkg/routes/health" "app/pkg/routes/resume" "app/pkg/routes/root" + + "github.com/charmbracelet/log" ) var ( - hash string = "dev"; + rev string = "foo"; ) func main() { - serv := app.Server{Port: 8080, Hash: hash} + log.Info("Current revision: " + rev) + serv := app.Server{Port: 8080, Rev: rev} serv.AddRoute("GET /", root.Handler) serv.AddRoute("GET /health", health.Handler) diff --git a/pkg/app/app.go b/pkg/app/app.go index 3d24b64..444e3a0 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -14,7 +14,7 @@ import ( type Server struct { Port int16 - Hash string + Rev string } func (serv *Server) Run() { @@ -35,13 +35,13 @@ func (serv *Server) AddRoute(route string, handler func(app *Server, w http.Resp w.Header().Add("Cache-Control", "no-cache, must-revalidate") hash := r.Header.Get("If-None-Match") // Already cached - if hash == serv.Hash { + if hash == serv.Rev { log.Info(fmt.Sprintf("Cached %s", log_fmt)) w.WriteHeader(http.StatusNotModified) return } - w.Header().Add("ETag", serv.Hash) + w.Header().Add("ETag", serv.Rev) comp, err := handler(serv, w, r) if err == nil { diff --git a/pkg/app/template.templ b/pkg/app/template.templ index 617e225..f6e8d97 100644 --- a/pkg/app/template.templ +++ b/pkg/app/template.templ @@ -7,14 +7,7 @@ import ( ) templ NavButton(href string, text string) { - + { text } } @@ -54,8 +47,17 @@ templ (app *Server) Template(body templ.Component) { opinions listed here are my own and not representative of any of my employers, past, future, and/or present.
- Served by me using Nix and Docker. Current build hash: "{ app.Hash }", source code available
- here.
+ Served by me using Nix and Docker.
+
+ Build rev:
+
+ { app.Rev }
+