From a72c0c275a962153a40bc805e5ce3ac7a273f55c Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 23 May 2026 22:54:30 +0200 Subject: Embed frontend to server executable This makes use of the `embed` package of the Go standard library to embed the full Hugo static website as a global filesystem variable inside the executable. This effectively makes all files of the static website part of the Go executable. That way it is always possible to just copy the server executable to a host, make it executable and run it without any other dependencies. --- handlers.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'handlers.go') diff --git a/handlers.go b/handlers.go index 4db3879..1ca96d8 100644 --- a/handlers.go +++ b/handlers.go @@ -1,13 +1,25 @@ package main import ( + "embed" "fmt" + "io/fs" + "log" "net/http" ) +//go:embed frontend/public +var frontendEmbed embed.FS + func init() { - router.Handle("/", http.FileServer(http.Dir("frontend/public"))) router.HandleFunc("/api/version", version) + + // frontend must come last to make sure /api takes precedence + frontend, err := fs.Sub(frontendEmbed, "frontend/public") + if err != nil { + log.Fatalf("No embedded frontend: %v", err) + } + router.PathPrefix("/").Handler(http.FileServerFS(frontend)) } func version(w http.ResponseWriter, r *http.Request) { -- cgit v1.3