diff options
| author | xengineering <me@xengineering.eu> | 2026-05-23 22:54:30 +0200 |
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2026-05-23 22:54:30 +0200 |
| commit | a72c0c275a962153a40bc805e5ce3ac7a273f55c (patch) | |
| tree | 8980c382e88fb48f024cbe507c2c55381d4d594f | |
| parent | 7bb61037ffa36549661b8a34e305343d40007ca2 (diff) | |
| download | finserv-a72c0c275a962153a40bc805e5ce3ac7a273f55c.tar finserv-a72c0c275a962153a40bc805e5ce3ac7a273f55c.tar.zst finserv-a72c0c275a962153a40bc805e5ce3ac7a273f55c.zip | |
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.
| -rw-r--r-- | handlers.go | 14 |
1 files changed, 13 insertions, 1 deletions
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) { |
