core/server.go

59 lines
1.2 KiB
Go

package main
import (
"fmt"
"os"
"system-trace/core/constants"
"system-trace/core/environment"
"system-trace/core/types"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
)
var Instance *fiber.App
func serveApp() {
Instance = initServer()
Instance.Use(recover.New())
Instance.Use(cors.New())
// Instance.Use(middlewares.ValidateSession)
initRouter(Instance)
Instance.Use(func(c *fiber.Ctx) error {
return c.
Status(fiber.StatusNotFound).
JSON(types.JSONError{
Error: constants.NOT_FOUND,
})
})
printRoutes(Instance)
Instance.Listen(":" + os.Getenv("APP_PORT"))
}
func initServer() *fiber.App {
return fiber.New(fiber.Config{
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
CaseSensitive: true,
Prefork: true,
AppName: "System Trace API",
ErrorHandler: func(c *fiber.Ctx, err error) error {
return c.Status(fiber.StatusInternalServerError).JSON(types.JSONError{
Error: err.Error(),
})
},
})
}
func printRoutes(app *fiber.App) {
debug := environment.IsDebug()
if !fiber.IsChild() && debug {
data, _ := json.MarshalIndent(app.GetRoutes(true), "", " ")
fmt.Print(string(data))
}
}