From 2dc210a7ee7272f241f6d159ccca01c4da4228d2 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 21 Jun 2026 10:49:11 +0200 Subject: Do not use init for HTTP handler registration This does not allow to use a *sql.DB which is initialized in main() inside to pass it to handlers with DB access. --- handlers.go | 8 +++++--- main.go | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/handlers.go b/handlers.go index 141230d..d28a491 100644 --- a/handlers.go +++ b/handlers.go @@ -6,20 +6,22 @@ import ( "io/fs" "log" "net/http" + + "github.com/gorilla/mux" ) //go:embed build/frontend/public var frontendEmbed embed.FS -func init() { - router.HandleFunc("/api/version", version) +func registerHandlers(r *mux.Router) { + r.HandleFunc("/api/version", version) // frontend must come last to make sure /api takes precedence frontend, err := fs.Sub(frontendEmbed, "build/frontend/public") if err != nil { log.Fatalf("No embedded frontend: %v.", err) } - router.PathPrefix("/").Handler(http.FileServerFS(frontend)) + r.PathPrefix("/").Handler(http.FileServerFS(frontend)) } func version(w http.ResponseWriter, r *http.Request) { diff --git a/main.go b/main.go index 0c84456..d0c4763 100644 --- a/main.go +++ b/main.go @@ -64,6 +64,8 @@ func run( log.Fatalf("Failed to migrate SQLite3 database '%s': %v", dbPath, err) } + registerHandlers(router) + server := &http.Server{ Handler: handler, Addr: addr, -- cgit v1.3.1