From 026b62851d625188df29d2744bf9e2e414a651fc Mon Sep 17 00:00:00 2001 From: Redume Date: Sun, 30 Oct 2022 20:12:26 +0300 Subject: [PATCH] added group for api and custom error --- functions/logger.go | 13 ++++++++++--- main.go | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/functions/logger.go b/functions/logger.go index eb5189b..3f25309 100644 --- a/functions/logger.go +++ b/functions/logger.go @@ -1,6 +1,7 @@ package functions import ( + "fmt" "os" "runtime" "strings" @@ -20,7 +21,8 @@ func Logger(text string) { } now := time.Now() - _, file, _, ok := runtime.Caller(1) + _, file, line, ok := runtime.Caller(1) + if !ok { file = "???" } @@ -31,10 +33,15 @@ func Logger(text string) { } if strings.Contains(file, dir) { - file = file[len(dir)+1:] + file = strings.Replace(file, dir, "", -1) } - _, err = f.Write([]byte(now.Format("Mon Jan 2 15:04:05 2006") + " | " + text + " [" + file + "] " + "\n")) + var lineString string + if ok { + lineString = fmt.Sprintf("%d", line) + } + + _, err = f.Write([]byte(now.Format("Mon Jan 2 15:04:05 2006") + " | " + text + " [" + file + "] [" + lineString + "]\n")) if err != nil { panic(err) } diff --git a/main.go b/main.go index 0fdbf2d..5a2d450 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "github.com/Redume/EveryNasa/api/controllers" "github.com/Redume/EveryNasa/functions" "github.com/Redume/EveryNasa/web/page" - "github.com/getlantern/systray" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" @@ -16,6 +15,22 @@ func main() { go functions.StartWallpaper() app := fiber.New() + app = fiber.New(fiber.Config{ + ErrorHandler: func(ctx *fiber.Ctx, err error) error { + code := fiber.StatusInternalServerError + + if e, ok := err.(*fiber.Error); ok { + code = e.Code + } + + if code == fiber.StatusNotFound { + return ctx.SendFile("./web/errors/404.html") + } + + return nil + }, + }) + app.Static("/static", "./web/static") app.Use(cors.New()) @@ -29,29 +44,32 @@ func main() { return page.About(c) }) - app.Post("/api/update/settings", func(c *fiber.Ctx) error { + api := app.Group("/api") + + update := api.Group("/update") + get := api.Group("/get") + create := api.Group("/create") + + update.Post("/settings", func(c *fiber.Ctx) error { return controllers.SettingsUpdate(c) }) - app.Post("/api/update/wallpaper", func(c *fiber.Ctx) error { + update.Post("/wallpaper", func(c *fiber.Ctx) error { return controllers.WallpaperUpdate(c) }) - app.Post("/api/update/startup", func(c *fiber.Ctx) error { + update.Post("/startup", func(c *fiber.Ctx) error { return controllers.Startup(c) }) - app.Post("/api/create/label", func(c *fiber.Ctx) error { + + create.Post("/label", func(c *fiber.Ctx) error { return controllers.CreateLabel(c) }) - app.Get("/api/get/settings", func(c *fiber.Ctx) error { + get.Get("/settings", func(c *fiber.Ctx) error { return controllers.SettingsGet(c) }) - app.Use(func(c *fiber.Ctx) error { - err := c.SendStatus(404) - if err != nil { - functions.Logger(err.Error()) - } - return c.SendFile("./web/errors/404.html") + get.Get("/settings", func(c *fiber.Ctx) error { + return controllers.SettingsGet(c) }) err := app.Listen(":3000")