- 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`.
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"jd-book-uploader/services"
|
|
)
|
|
|
|
// HealthCheck handles health check requests
|
|
func HealthCheck(c *fiber.Ctx) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
status := fiber.Map{
|
|
"status": "ok",
|
|
}
|
|
|
|
// Check database connection
|
|
if services.DB != nil {
|
|
err := services.DB.Ping(ctx)
|
|
if err != nil {
|
|
status["database"] = "disconnected"
|
|
status["status"] = "degraded"
|
|
return c.Status(fiber.StatusServiceUnavailable).JSON(status)
|
|
}
|
|
status["database"] = "connected"
|
|
} else {
|
|
status["database"] = "not_initialized"
|
|
status["status"] = "degraded"
|
|
return c.Status(fiber.StatusServiceUnavailable).JSON(status)
|
|
}
|
|
|
|
// Check Firebase connection
|
|
if services.FirebaseClient != nil {
|
|
// Try to get bucket to verify connection
|
|
bucket, err := services.FirebaseClient.DefaultBucket()
|
|
if err != nil || bucket == nil {
|
|
status["firebase"] = "disconnected"
|
|
status["status"] = "degraded"
|
|
return c.Status(fiber.StatusServiceUnavailable).JSON(status)
|
|
}
|
|
status["firebase"] = "connected"
|
|
} else {
|
|
status["firebase"] = "not_initialized"
|
|
status["status"] = "degraded"
|
|
return c.Status(fiber.StatusServiceUnavailable).JSON(status)
|
|
}
|
|
|
|
return c.JSON(status)
|
|
}
|