Add user auth, cart, checkout and personal account for guests and registered users

This commit is contained in:
shop
2026-06-25 17:07:02 +03:00
parent 08727a1995
commit efc95d48c4
16 changed files with 1528 additions and 159 deletions
+32 -13
View File
@@ -35,6 +35,9 @@ func main() {
productRepo := repository.NewProductRepository(pool)
adminRepo := repository.NewAdminRepository(pool)
statsRepo := repository.NewStatsRepository(pool)
userRepo := repository.NewUserRepository(pool)
cartRepo := repository.NewCartRepository(pool)
orderRepo := repository.NewOrderRepository(pool)
storage, err := upload.NewStorage(cfg.UploadDir)
if err != nil {
@@ -46,29 +49,47 @@ func main() {
log.Fatalf("admin seed: %v", err)
}
log.Printf("admin ready: %s", cfg.AdminEmail)
} else {
log.Println("warning: set ADMIN_EMAIL and ADMIN_PASSWORD in .env to create admin")
}
home, err := handlers.NewHomeHandler(productRepo, statsRepo)
tmpl, err := handlers.ParseTemplates()
if err != nil {
log.Fatalf("handler: %v", err)
log.Fatalf("templates: %v", err)
}
session := auth.NewSession(cfg.SessionSecret)
admin := handlers.NewAdminHandler(adminRepo, productRepo, statsRepo, storage, session, home.Templates())
adminSession := auth.NewSession(cfg.SessionSecret, "shop_admin_session", "/admin")
userSession := auth.NewSession(cfg.SessionSecret, "shop_user_session", "/")
cartSession := auth.NewSession(cfg.SessionSecret, "shop_cart_key", "/")
customer := handlers.NewCustomerHandler(
productRepo, userRepo, cartRepo, orderRepo, statsRepo,
userSession, cartSession, tmpl,
)
admin := handlers.NewAdminHandler(adminRepo, productRepo, statsRepo, storage, adminSession, tmpl)
mux := http.NewServeMux()
mux.Handle("GET /static/", http.StripPrefix("/static/", home.Static()))
mux.Handle("GET /static/", http.StripPrefix("/static/", handlers.StaticHandler()))
mux.Handle("GET /uploads/", storage.FileServer())
mux.HandleFunc("GET /health", home.Health)
mux.HandleFunc("GET /{$}", home.Home)
mux.HandleFunc("GET /health", customer.Health)
mux.HandleFunc("GET /{$}", customer.Home)
mux.HandleFunc("GET /register", customer.RegisterPage)
mux.HandleFunc("POST /register", customer.Register)
mux.HandleFunc("GET /login", customer.LoginPage)
mux.HandleFunc("POST /login", customer.Login)
mux.HandleFunc("POST /logout", customer.Logout)
mux.HandleFunc("GET /cart", customer.CartPage)
mux.HandleFunc("POST /cart/add", customer.CartAdd)
mux.HandleFunc("POST /cart/update", customer.CartUpdate)
mux.HandleFunc("POST /cart/remove", customer.CartRemove)
mux.HandleFunc("GET /checkout", customer.CheckoutPage)
mux.HandleFunc("POST /checkout", customer.Checkout)
mux.HandleFunc("GET /account", customer.Account)
mux.HandleFunc("POST /account", customer.Account)
mux.HandleFunc("GET /admin/login", admin.LoginPage)
mux.HandleFunc("POST /admin/login", admin.Login)
mux.HandleFunc("POST /admin/logout", admin.Logout)
mux.HandleFunc("GET /admin/{$}", admin.Dashboard)
mux.HandleFunc("GET /admin/products", admin.ProductList)
mux.HandleFunc("GET /admin/products/new", admin.ProductNew)
mux.HandleFunc("POST /admin/products/save", admin.ProductSave)
@@ -98,9 +119,7 @@ func main() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("shutdown: %v", err)
}
_ = srv.Shutdown(shutdownCtx)
}
func getEnv(key, fallback string) string {