From 869fb9691f877116d5b15a92de006d0daf4d70e5 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Thu, 17 Oct 2024 00:27:08 +0200 Subject: Restructure and change to chi as muxing framework --- main.go | 87 ++++++++++++++++++++--------------------------------------------- 1 file changed, 26 insertions(+), 61 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 05ee948..34a6719 100644 --- a/main.go +++ b/main.go @@ -6,16 +6,16 @@ import ( "net/http" "os" - "github.com/gorilla/handlers" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" "github.com/jackc/pgx/v5/pgxpool" "github.com/joho/godotenv" - "gosten/model" - "gosten/templ" + "gosten/csrf" + "gosten/pages" + "gosten/session" ) -var Q *model.Queries - func checkEnvEntry(e string) { if os.Getenv(e) == "" { log.Fatalf("Variable '%s' not set", e) @@ -43,66 +43,31 @@ func main() { } defer db.Close() - Q = model.New(db) - - mux := http.NewServeMux() - - // handlers that DO NOT require authentification - mux.Handle("GET /login", loginPage()) - mux.HandleFunc("POST /login", handleLogin) - mux.Handle("GET /logout", handleLogout()) - mux.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static")))) - mux.Handle("/favicon.ico", http.NotFoundHandler()) - - handler := sessionHandler(csrfHandler(mux)) - handler = handlers.CombinedLoggingHandler(os.Stderr, handler) - handler = handlers.ProxyHeaders(handler) + pages.Connect(db) - // setup authentification - authMux := http.NewServeMux() - mux.Handle("/", RequireAuth(authMux)) + router := chi.NewRouter() - // handlers that required authentification - authMux.Handle("GET /{$}", indexPage()) - authMux.Handle("GET /recur/{$}", recurPage()) - authMux.Handle("GET /categories/{$}", categoriesPage()) - authMux.Handle("GET /", notfound()) - - log.Fatal(http.ListenAndServe(os.Getenv("GOSTEN_ADDRESS"), handler)) -} - -func showTemplate(w http.ResponseWriter, tpl string, data any) { - if err := templ.Lookup(tpl).Execute(w, data); err != nil { - log.Panicf("Executing '%s' with %+v: %v", tpl, data, err) - } -} + // A good base middleware stack + router.Use(middleware.RequestID) + router.Use(middleware.RealIP) + router.Use(middleware.CleanPath) + router.Use(middleware.Logger) + router.Use(middleware.Recoverer) -func indexPage() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - uid := userId(r) - u, _ := Q.GetUserById(r.Context(), uid) - showTemplate(w, "index", u.Name) - } -} + // handlers that DO NOT require authentification + router.Handle("/static/*", http.StripPrefix("/static", http.FileServer(http.Dir("static")))) + router.Get("/favicon.ico", http.NotFound) -func recurPage() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - uid := userId(r) - exps, _ := Q.GetRecurExpenses(r.Context(), uid) - showTemplate(w, "recur", exps) - } -} + appRouter := router.With(csrf.Handler(), session.Handler()) + appRouter.Get("/login", pages.Login()) + appRouter.Post("/login", pages.HandleLogin) + appRouter.Get("/logout", pages.Logout()) -func categoriesPage() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - uid := userId(r) - cats, _ := Q.GetCategoriesOrdered(r.Context(), uid) - showTemplate(w, "categories", cats) - } -} + authRouter := appRouter.With(pages.RequireAuth) + authRouter.Mount("/", pages.Init()) + authRouter.Mount("/recur", pages.Recur()) + authRouter.Mount("/categories", pages.Categories()) + authRouter.NotFound(pages.NotFound()) -func notfound() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - showTemplate(w, "404", r.RequestURI) - } + log.Fatal(http.ListenAndServe(os.Getenv("GOSTEN_ADDRESS"), router)) } -- cgit v1.2.3-70-g09d2