diff options
| author | roerick <roerick@wyo.town> | 2025-10-19 14:24:40 -0600 |
|---|---|---|
| committer | roerick <roerick@wyo.town> | 2025-10-19 14:24:40 -0600 |
| commit | 132e0fe607396123a9fc5390c8657ef145bba3c6 (patch) | |
| tree | 9a420d6f89a2de70d17bfb08789a26ccc501a9b9 /main.go | |
| parent | a1c3883334a9c54358173ebd822a5a85036dcda6 (diff) | |
initial commit with auth working
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 46 |
1 files changed, 34 insertions, 12 deletions
@@ -2,11 +2,12 @@ package main import ( "log" - "fmt" "net/http" "wyo.town/netzah/models" "wyo.town/netzah/handlers" + "wyo.town/netzah/session" + "wyo.town/netzah/middleware" "github.com/gorilla/mux" "gorm.io/driver/postgres" @@ -19,27 +20,48 @@ func init() { if err != nil { panic("failed to connect database") } - db.AutoMigrate(&models.User{}) db.AutoMigrate(&models.Blog{}) - db.AutoMigrate(&models.Comment{}) } + + + func main() { + + session.Init() + // 2) Router + attach SCS middleware correctly r := mux.NewRouter() + r.Use(session.Manager.LoadAndSave) + // 3) Routes r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("Hello, World!")) + _, _ = w.Write([]byte("Hello, World!")) }) // Blog endpoints - r.HandleFunc("/blogs", handlers.CreateBlog).Methods("GET") - r.HandleFunc("/blogs", handlers.StoreBlog).Methods("POST") - r.HandleFunc("/blogs/{id}", handlers.GetBlog).Methods("GET") - // r.HandleFunc("/blogs/{id}", handlers.UpdateBlog).Methods("PUT") - // r.HandleFunc("/blogs/{id}", handlers.DeleteBlog).Methods("DELETE") // Also consider adding to handle a form submission for deletion - fmt.Println("Starting server on :8090") - if err := http.ListenAndServe(":8090", r); err != nil { - log.Fatal(err) + 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) + + r.HandleFunc("/login", handlers.LoginGET) + r.HandleFunc("/login/submit", handlers.LoginPOST) + + // Logout (POST is recommended; allow GET only if you must) + r.HandleFunc("/logout", handlers.LogoutPOST) + + // r.HandleFunc("/blogs/{id}", handlers.UpdateBlog).Methods(http.MethodPut) + // r.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))) + + // 4) Start the server using the router you wrapped + log.Println("Starting server on :8090") + srv := &http.Server{ + Addr: ":8090", + Handler: r, } + log.Fatal(srv.ListenAndServe()) } + |
