Files
jd-book-uploader-backend/main.go
ianshaloom ebeae34e01 Add initial project structure with core functionality for book and stationery uploads
- Created main application entry point in `main.go`.
- Added configuration management in `config/config.go` and tests in `config/config_test.go`.
- Implemented handlers for book and stationery uploads in `handlers/book.go` and `handlers/stationery.go`, including validation logic.
- Established database connection and services in `services/database.go` and `services/book_service.go`.
- Defined models for books and stationery in `models/book.go` and `models/stationery.go`.
- Set up Firebase integration for image uploads in `services/firebase.go`.
- Created migration scripts for database schema in `migrations/001_create_tables.sql` and subsequent updates.
- Added CORS and error handling middleware.
- Included comprehensive tests for handlers, services, and utilities.
- Documented API endpoints and usage in `README.md` and migration instructions in `migrations/README.md`.
- Introduced `.gitignore` to exclude unnecessary files and directories.
- Added Go module support with `go.mod` and `go.sum` files.
- Implemented utility functions for slug generation and validation in `utils/slug.go` and `utils/validation.go`.
2025-11-21 08:50:27 +03:00

88 lines
1.9 KiB
Go

package main
import (
"log"
"os"
"os/signal"
"syscall"
"jd-book-uploader/config"
"jd-book-uploader/handlers"
"jd-book-uploader/middleware"
"jd-book-uploader/services"
"github.com/gofiber/fiber/v2"
)
func main() {
// Load configuration
cfg, err := config.LoadConfig()
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
// Initialize database connection
_, err = services.NewDBPool(cfg)
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
defer services.CloseDB()
log.Println("Database connected successfully")
// Initialize Firebase
_, err = services.InitFirebase(cfg)
if err != nil {
log.Fatalf("Failed to initialize Firebase: %v", err)
}
log.Println("Firebase initialized successfully")
// Create Fiber app
app := fiber.New(fiber.Config{
AppName: "JD Book Uploader API",
})
// Register middleware
app.Use(middleware.RecoverHandler()) // Recover from panics
app.Use(middleware.ErrorHandler) // Global error handler
app.Use(middleware.SetupCORS(cfg)) // CORS
app.Use(middleware.SetupLogger()) // Request logging
// Register routes
api := app.Group("/api")
api.Get("/health", handlers.HealthCheck)
api.Post("/books", handlers.UploadBook)
api.Post("/stationery", handlers.UploadStationery)
// Start server
port := cfg.Port
if port == "" {
port = "8080"
}
// Graceful shutdown
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
log.Printf("Server starting on port %s", port)
if err := app.Listen(":" + port); err != nil {
log.Fatalf("Server failed to start: %v", err)
}
}()
<-c
log.Println("Gracefully shutting down...")
// Close database connection
if err := services.CloseDB(); err != nil {
log.Printf("Error closing database: %v", err)
}
// Shutdown Fiber app
if err := app.Shutdown(); err != nil {
log.Fatalf("Server shutdown failed: %v", err)
}
log.Println("Server stopped")
}