summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go35
1 files changed, 18 insertions, 17 deletions
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())
}