summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2026-06-21 10:49:11 +0200
committerxengineering <me@xengineering.eu>2026-06-21 10:49:11 +0200
commit2dc210a7ee7272f241f6d159ccca01c4da4228d2 (patch)
tree3e8f10c0da7321146453d22a9d741dc98f28d675
parentb4950dfa73802afb63009f3ef501463baf5c30e2 (diff)
downloadfinserv-2dc210a7ee7272f241f6d159ccca01c4da4228d2.tar
finserv-2dc210a7ee7272f241f6d159ccca01c4da4228d2.tar.zst
finserv-2dc210a7ee7272f241f6d159ccca01c4da4228d2.zip
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.
-rw-r--r--handlers.go8
-rw-r--r--main.go2
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,