summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2024-10-17 00:27:08 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2024-10-17 00:27:08 +0200
commit869fb9691f877116d5b15a92de006d0daf4d70e5 (patch)
tree2493c72172d5817ec9deec36229a84b687eb3190 /main.go
parent6fc180ba6d9bc5c32340466988d9e26f8d6e3c5c (diff)
downloadgosten-869fb9691f877116d5b15a92de006d0daf4d70e5.tar.gz
gosten-869fb9691f877116d5b15a92de006d0daf4d70e5.tar.bz2
gosten-869fb9691f877116d5b15a92de006d0daf4d70e5.zip
Restructure and change to chi as muxing framework
Diffstat (limited to 'main.go')
-rw-r--r--main.go87
1 files changed, 26 insertions, 61 deletions
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))
}