- 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`.
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// ErrorHandler is a global error handler middleware
|
|
func ErrorHandler(c *fiber.Ctx) error {
|
|
// Call next middleware/handler
|
|
err := c.Next()
|
|
|
|
// If no error, return
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
|
|
// Log error with context
|
|
log.Printf("Error: %v | Method: %s | Path: %s | IP: %s",
|
|
err,
|
|
c.Method(),
|
|
c.Path(),
|
|
c.IP(),
|
|
)
|
|
|
|
// Check if error is a Fiber error
|
|
code := fiber.StatusInternalServerError
|
|
message := "Internal Server Error"
|
|
|
|
if e, ok := err.(*fiber.Error); ok {
|
|
code = e.Code
|
|
message = e.Message
|
|
}
|
|
|
|
// Return structured error response
|
|
return c.Status(code).JSON(fiber.Map{
|
|
"success": false,
|
|
"error": message,
|
|
})
|
|
}
|
|
|
|
// RecoverHandler recovers from panics and returns a proper error response
|
|
func RecoverHandler() fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
// Log panic with context
|
|
log.Printf("Panic recovered: %v | Method: %s | Path: %s | IP: %s",
|
|
r,
|
|
c.Method(),
|
|
c.Path(),
|
|
c.IP(),
|
|
)
|
|
|
|
// Return error response
|
|
c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"error": "Internal Server Error",
|
|
})
|
|
}
|
|
}()
|
|
|
|
return c.Next()
|
|
}
|
|
}
|