From 68ff409afc5930ce8c76ca1ac323c7180789416e Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Thu, 4 Apr 2024 09:37:28 +0200 Subject: [PATCH] Make router better --- handlers/app.handlers.go | 4 ++++ handlers/routes.go | 14 +++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/handlers/app.handlers.go b/handlers/app.handlers.go index 3fd6297..8ea6872 100644 --- a/handlers/app.handlers.go +++ b/handlers/app.handlers.go @@ -44,6 +44,10 @@ type AppHandler struct { } func (bh *AppHandler) appHandler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.Redirect(w, r, "/", http.StatusFound) + return + } bookmarks := bh.bookmarkService.GetAllBookmarks() staticSystem := bh.systemService.GetStaticInformation() liveSystem := bh.systemService.GetLiveInformation() diff --git a/handlers/routes.go b/handlers/routes.go index 706cf26..8adda3f 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -8,17 +8,17 @@ import ( func SetupRoutes(router *http.ServeMux, sse *sse.Server, appHandler *AppHandler, authHandler *AuthHandler) { if authHandler.sessionManager != nil { - router.HandleFunc("/sign-in", authHandler.signInHandler) - router.HandleFunc("/sign-in-callback", authHandler.signInCallbackHandler) - router.HandleFunc("/sign-out", authHandler.signOutHandler) + router.HandleFunc("GET /sign-in", authHandler.signInHandler) + router.HandleFunc("GET /sign-in-callback", authHandler.signInCallbackHandler) + router.HandleFunc("GET /sign-out", authHandler.signOutHandler) } - router.Handle("/sse", authHandler.authRequired(http.HandlerFunc(sse.ServeHTTP))) + router.Handle("GET /sse", authHandler.authRequired(http.HandlerFunc(sse.ServeHTTP))) fsAssets := http.FileServer(http.Dir("assets")) - router.Handle("/assets/", authHandler.authRequired(http.StripPrefix("/assets/", fsAssets))) + router.Handle("GET /assets/", authHandler.authRequired(http.StripPrefix("/assets/", fsAssets))) fsIcons := http.FileServer(http.Dir("storage/icons")) - router.Handle("/storage/icons/", authHandler.authRequired(http.StripPrefix("/storage/icons/", fsIcons))) + router.Handle("GET /storage/icons/", authHandler.authRequired(http.StripPrefix("/storage/icons/", fsIcons))) - router.Handle("/", authHandler.authRequired(http.HandlerFunc(appHandler.appHandler))) + router.Handle("GET /", authHandler.authRequired(http.HandlerFunc(appHandler.appHandler))) }