Add admin panel: visit counter, product editor, screenshots upload

This commit is contained in:
shop
2026-06-25 16:59:21 +03:00
parent 6322aee714
commit 08727a1995
21 changed files with 1209 additions and 67 deletions
+19 -4
View File
@@ -15,6 +15,7 @@ import (
"shop/internal/handlers"
"shop/internal/middleware"
"shop/internal/repository"
"shop/internal/upload"
)
func main() {
@@ -33,6 +34,12 @@ func main() {
productRepo := repository.NewProductRepository(pool)
adminRepo := repository.NewAdminRepository(pool)
statsRepo := repository.NewStatsRepository(pool)
storage, err := upload.NewStorage(cfg.UploadDir)
if err != nil {
log.Fatalf("upload storage: %v", err)
}
if cfg.AdminEmail != "" && cfg.AdminPassword != "" {
if err := adminRepo.Upsert(ctx, cfg.AdminEmail, cfg.AdminPassword); err != nil {
@@ -43,16 +50,17 @@ func main() {
log.Println("warning: set ADMIN_EMAIL and ADMIN_PASSWORD in .env to create admin")
}
home, err := handlers.NewHomeHandler(productRepo)
home, err := handlers.NewHomeHandler(productRepo, statsRepo)
if err != nil {
log.Fatalf("handler: %v", err)
}
session := auth.NewSession(cfg.SessionSecret)
admin := handlers.NewAdminHandler(adminRepo, productRepo, session, home.Templates())
admin := handlers.NewAdminHandler(adminRepo, productRepo, statsRepo, storage, session, home.Templates())
mux := http.NewServeMux()
mux.Handle("GET /static/", http.StripPrefix("/static/", home.Static()))
mux.Handle("GET /uploads/", storage.FileServer())
mux.HandleFunc("GET /health", home.Health)
mux.HandleFunc("GET /{$}", home.Home)
@@ -61,12 +69,19 @@ func main() {
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)
mux.HandleFunc("GET /admin/products/{id}/edit", admin.ProductEdit)
mux.HandleFunc("POST /admin/products/{id}/delete", admin.ProductDelete)
mux.HandleFunc("POST /admin/products/{id}/images/{imageId}/delete", admin.ImageDelete)
addr := ":" + getEnv("APP_PORT", "8080")
srv := &http.Server{
Addr: addr,
Handler: middleware.TrustProxy(mux),
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
ReadTimeout: 15 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 60 * time.Second,
}