From 56b77ad18c90e47299c46d6f0fde8abe3ff6d16d Mon Sep 17 00:00:00 2001 From: roerick Date: Sun, 19 Oct 2025 15:50:47 -0600 Subject: switch to mux prefix for router --- main.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index f381fa8..e39e8c5 100644 --- a/main.go +++ b/main.go @@ -29,38 +29,39 @@ func init() { func main() { session.Init() - // 2) Router + attach SCS middleware correctly - r := mux.NewRouter() - r.Use(session.Manager.LoadAndSave) + mux := mux.NewRouter() + mux.Use(session.Manager.LoadAndSave) - // 3) Routes - r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte("Hello, World!")) }) - // Blog endpoints - r.HandleFunc("/blogs", handlers.CreateBlog).Methods(http.MethodGet) - r.HandleFunc("/blogs", handlers.StoreBlog).Methods(http.MethodPost) - r.HandleFunc("/blogs/{id}", handlers.GetBlog).Methods(http.MethodGet) + mux.HandleFunc("/blogs", handlers.CreateBlog).Methods(http.MethodGet) + mux.HandleFunc("/blogs", handlers.StoreBlog).Methods(http.MethodPost) + mux.HandleFunc("/blogs/{id}", handlers.GetBlog).Methods(http.MethodGet) - r.HandleFunc("/login", handlers.LoginGET) - r.HandleFunc("/login/submit", handlers.LoginPOST) + mux.HandleFunc("GET /posts/{slug}", func(w http.ResponseWriter, r *http.Request) { + slug := r.PathValue("slug") + fmt.Fprintf(w, "Post: %s", slug) + }) + + mux.HandleFunc("/login", handlers.LoginGET) + mux.HandleFunc("/login/submit", handlers.LoginPOST) // Logout (POST is recommended; allow GET only if you must) - r.HandleFunc("/logout", handlers.LogoutPOST) + mux.HandleFunc("/logout", handlers.LogoutPOST) - // r.HandleFunc("/blogs/{id}", handlers.UpdateBlog).Methods(http.MethodPut) - // r.HandleFunc("/blogs/{id}", handlers.DeleteBlog).Methods(http.MethodDelete) + // mux.HandleFunc("/blogs/{id}", handlers.UpdateBlog).Methods(http.MethodPut) + // mux.HandleFunc("/blogs/{id}", handlers.DeleteBlog).Methods(http.MethodDelete) // Example protected route (SCS must already be on the chain) - r.Handle("/whoami", middleware.RequireAuth(http.HandlerFunc(handlers.Dashboard))) + mux.Handle("/whoami", middleware.RequireAuth(http.HandlerFunc(handlers.Dashboard))) - // 4) Start the server using the router you wrapped log.Println("Starting server on :8090") srv := &http.Server{ Addr: ":8090", - Handler: r, + Handler: mux, } log.Fatal(srv.ListenAndServe()) } -- cgit v1.2.3