diff --git a/cmd/server/main.go b/cmd/server/main.go index 5e5e726..11ad79d 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/davidbyttow/govips/v2/vips" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/recover" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/joho/godotenv" "github.com/nezuchan/image-proxy/impl/image_resizer/client" @@ -24,7 +25,27 @@ func main() { defer vips.Shutdown() _ = godotenv.Load() - app := fiber.New() + app := fiber.New(fiber.Config{ + Prefork: true, + ServerHeader: "Fiber", + ErrorHandler: func(ctx *fiber.Ctx, err error) error { + // Status code defaults to 500 + code := fiber.StatusInternalServerError + + // Retrieve the custom status code if it's a *fiber.Error + var e *fiber.Error + if errors.As(err, &e) { + code = e.Code + } + + return ctx.Status(code).JSON(&struct_type.ErrorResponse{ + Message: "Something went wrong, please try again later.", + Error: err.Error(), + }) + }, + }) + + app.Use(recover.New()) app.Use(logger.New(logger.Config{ Format: "[${ip}]:${port} [${latency}] ${status} - ${method} ${path}\n",