From 7066367038f354c663db2ffbe3db7b8b0adb3d38 Mon Sep 17 00:00:00 2001 From: Tibz-Dankan Date: Mon, 19 Aug 2024 22:10:37 +0300 Subject: [PATCH] feat: add custom not found route handler --- internal/routes/notFound.go | 17 +++++++++++++++++ internal/routes/router.go | 3 +++ 2 files changed, 20 insertions(+) create mode 100644 internal/routes/notFound.go diff --git a/internal/routes/notFound.go b/internal/routes/notFound.go new file mode 100644 index 0000000..3b3660f --- /dev/null +++ b/internal/routes/notFound.go @@ -0,0 +1,17 @@ +package routes + +import ( + "net/http" + + "github.com/Tibz-Dankan/keep-active/internal/services" + "github.com/gorilla/mux" +) + +func notFound(w http.ResponseWriter, r *http.Request) { + message := "Endpoint " + "'" + r.URL.Path + "'" + " does not exist!" + services.AppError(message, 404, w) +} + +func NotFoundRoute(router *mux.Router) { + router.NotFoundHandler = http.HandlerFunc(notFound) +} diff --git a/internal/routes/router.go b/internal/routes/router.go index 4c534f0..276ccf7 100644 --- a/internal/routes/router.go +++ b/internal/routes/router.go @@ -73,5 +73,8 @@ func AppRouter() *mux.Router { // Monitor route monitor.GetMetrics(router) + // Not found route + NotFoundRoute(router) + return router }