package main import ( "fmt" "net/http" "time" "components" //"db" "github.com/a-h/templ" "github.com/alexedwards/scs/v2" ) var sessionManager *scs.SessionManager func getHandler(w http.ResponseWriter, r *http.Request) { component := components.Dashboard("DerGrumpf") component.Render(r.Context(), w) } func postHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() username := r.Form["username"][0] component := components.Dashboard(username) component.Render(r.Context(), w) } func errorHandler(w http.ResponseWriter, r *http.Request, status int) { w.WriteHeader(status) if status == http.StatusNotFound { component := components.NotFound() component.Render(r.Context(), w) } } func testHandler(w http.ResponseWriter, r *http.Request) { component := components.Test() component.Render(r.Context(), w) } func initMux() *http.ServeMux { mux := http.NewServeMux() // Index Handle mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { errorHandler(w, r, http.StatusNotFound) return } if r.Method == http.MethodPost { postHandler(w, r) return } getHandler(w, r) }) // File Server mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))) mux.HandleFunc("/test", testHandler) return mux } func main() { sessionManager := scs.New() sessionManager.Lifetime = 24 * time.Hour sessionManager.Cookie.Persist = false mux := initMux() component := components.Login() http.Handle("/", templ.Handler(component)) fmt.Println("Listening on http://127.0.0.1:3000") if err := http.ListenAndServe(":3000", sessionManager.LoadAndSave(mux)); err != nil { fmt.Println("Error listening: %v", err) } }