package main import ( "log" "net/http" "wyo.town/netzah/models" "wyo.town/netzah/handlers" "wyo.town/netzah/session" "wyo.town/netzah/middleware" "wyo.town/netzah/blog" "github.com/gorilla/mux" "gorm.io/driver/postgres" "gorm.io/gorm" ) func init() { dsn := "host=localhost user=_postgresql password=postGRES dbname=netzah port=5432 sslmode=disable TimeZone=US/Mountain client_encoding=UTF8" db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { panic("failed to connect database") } db.AutoMigrate(&models.Blog{}) } func loggingMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Printf("Incoming request: %s %s", r.Method, r.URL.Path) next.ServeHTTP(w, r) }) } func main() { session.Init() mux := mux.NewRouter() mux.Use(session.Manager.LoadAndSave) mux.Use(loggingMiddleware) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte("Hello, World!")) }) 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) mux.HandleFunc("/posts/{slug}", blog.PostHandler(blog.FileReader{})) mux.HandleFunc("/login", handlers.LoginGET) mux.HandleFunc("/login/submit", handlers.LoginPOST) mux.HandleFunc("/logout", handlers.LogoutPOST) // mux.HandleFunc("/blogs/{id}", handlers.UpdateBlog).Methods(http.MethodPut) // mux.HandleFunc("/blogs/{id}", handlers.DeleteBlog).Methods(http.MethodDelete) mux.Handle("/whoami", middleware.RequireAuth(http.HandlerFunc(handlers.Dashboard))) log.Println("Starting server on :8090") srv := &http.Server{ Addr: ":8090", Handler: mux, } log.Fatal(srv.ListenAndServe()) }