From 5c0741fdf601da3a45bc104a393051134f3aeb90 Mon Sep 17 00:00:00 2001 From: Jacob Gunther Date: Mon, 14 Aug 2023 21:33:59 -0500 Subject: [PATCH] Proper route declaration and error handling --- src/main.go | 28 +++++++--------------------- src/routes.go | 26 ++++++++++++++++++++------ 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/main.go b/src/main.go index 778b3b2..6eedd70 100644 --- a/src/main.go +++ b/src/main.go @@ -8,16 +8,19 @@ import ( "os" "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/cors" - "github.com/gofiber/fiber/v2/middleware/logger" - "github.com/gofiber/fiber/v2/middleware/recover" ) var ( app *fiber.App = fiber.New(fiber.Config{ DisableStartupMessage: true, ErrorHandler: func(ctx *fiber.Ctx, err error) error { - log.Printf("Error: %v - URI: %v\n", err, ctx.Request().URI()) + var fiberError *fiber.Error + + if errors.As(err, &fiberError) { + return ctx.SendStatus(fiberError.Code) + } + + log.Printf("Error: %v - URI: %s\n", err, ctx.Request().URI()) return ctx.SendStatus(http.StatusInternalServerError) }, @@ -56,23 +59,6 @@ func init() { log.Println("Successfully connected to Redis") } - app.Use(recover.New(recover.Config{ - EnableStackTrace: true, - })) - - if conf.Environment == "development" { - app.Use(cors.New(cors.Config{ - AllowOrigins: "*", - AllowMethods: "HEAD,OPTIONS,GET", - ExposeHeaders: "X-Cache-Hit,X-Cache-Time-Remaining", - })) - - app.Use(logger.New(logger.Config{ - Format: "${time} ${ip}:${port} -> ${status}: ${method} ${path} (${latency})\n", - TimeFormat: "2006/01/02 15:04:05", - })) - } - if instanceID, err = GetInstanceID(); err != nil { panic(err) } diff --git a/src/routes.go b/src/routes.go index b9091a3..2fec7a4 100644 --- a/src/routes.go +++ b/src/routes.go @@ -8,11 +8,31 @@ import ( "time" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cors" + "github.com/gofiber/fiber/v2/middleware/logger" + "github.com/gofiber/fiber/v2/middleware/recover" "github.com/mcstatus-io/mcutil" "github.com/mcstatus-io/mcutil/options" ) func init() { + app.Use(recover.New(recover.Config{ + EnableStackTrace: true, + })) + + if conf.Environment == "development" { + app.Use(cors.New(cors.Config{ + AllowOrigins: "*", + AllowMethods: "HEAD,OPTIONS,GET", + ExposeHeaders: "X-Cache-Hit,X-Cache-Time-Remaining", + })) + + app.Use(logger.New(logger.Config{ + Format: "${time} ${ip}:${port} -> ${status}: ${method} ${path} (${latency})\n", + TimeFormat: "2006/01/02 15:04:05", + })) + } + app.Get("/ping", PingHandler) app.Get("/favicon.ico", FaviconHandler) app.Get("/status/java/:address", JavaStatusHandler) @@ -20,7 +40,6 @@ func init() { app.Get("/icon", DefaultIconHandler) app.Get("/icon/:address", IconHandler) app.Post("/vote", SendVoteHandler) - app.Use(NotFoundHandler) } // PingHandler responds with a 200 OK status for simple health checks. @@ -154,8 +173,3 @@ func SendVoteHandler(ctx *fiber.Ctx) error { func DefaultIconHandler(ctx *fiber.Ctx) error { return ctx.Type("png").Send(assets.DefaultIcon) } - -// NotFoundHandler handles requests to routes that do not exist and returns a 404 Not Found status. -func NotFoundHandler(ctx *fiber.Ctx) error { - return ctx.SendStatus(http.StatusNotFound) -}