added group for api and custom error

This commit is contained in:
Данил 2022-10-30 20:12:26 +03:00
parent 2ee1052085
commit 026b62851d
2 changed files with 40 additions and 15 deletions

View file

@ -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)
}

42
main.go
View file

@ -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")