Skip to content

Commit

Permalink
feat: use git revision as build hash
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Oct 5, 2024
1 parent 787d435 commit 0b32d96
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
5 changes: 2 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
templ
tailwindcss
makeWrapper
nix
];
preBuild =
let
Expand All @@ -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 (
Expand Down Expand Up @@ -144,7 +143,7 @@
name = "buildapp";
text = ''
${packages.default.preBuild}
go build
go build -ldflags "$ldflags" -o app
./app
'';
})
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type Server struct {
Port int16
Hash string
Rev string
}

func (serv *Server) Run() {
Expand All @@ -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 {
Expand Down
22 changes: 12 additions & 10 deletions pkg/app/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ import (
)

templ NavButton(href string, text string) {
<a
hx-get={ href }
hx-trigger="click"
hx-target="main"
hx-swap="innerHTML"
hx-replace-url="true"
class="cursor-pointer"
>
<a hx-get={ href } hx-trigger="click" hx-target="main" hx-swap="innerHTML" hx-replace-url="true" class="cursor-pointer">
{ text }
</a>
}
Expand Down Expand Up @@ -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.
</p>
<p>
Served by me using Nix and Docker. Current build hash: "{ app.Hash }", source code available
<a href="https://github.com/drawbu/drawbu.dev" target="_blank" rel="noopener">here</a>.
Served by me using Nix and Docker.
<br/>
Build rev:
<a
target="_blank"
rel="noopener"
href={ templ.SafeURL("https://github.com/drawbu/drawbu.dev/tree/" +
app.Rev) }
>
{ app.Rev }
</a>
</p>
</footer>
</body>
Expand Down

0 comments on commit 0b32d96

Please sign in to comment.