Zeus/main.go

82 lines
2.0 KiB
Go
Raw Normal View History

2021-12-13 21:39:15 +08:00
package main
import (
2021-12-14 13:53:22 +08:00
"bgm/consts"
2021-12-13 21:39:15 +08:00
"bgm/handler"
"bgm/helper"
"bgm/model"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/requestid"
ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap"
"os"
"runtime"
)
const (
createCategory = "/sts"
)
2021-12-14 14:31:18 +08:00
type handleM map[string]func(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error
2021-12-13 21:39:15 +08:00
var (
2021-12-14 14:31:18 +08:00
mysqlMap = make(bgm.MysqlMap, 0)
2021-12-13 21:39:15 +08:00
err error
2021-12-14 14:31:18 +08:00
handlerMap = make(handleM, 0)
2021-12-13 21:39:15 +08:00
app = fiber.New(fiber.Config{
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
return ctx.Status(fiber.StatusInternalServerError).JSON(model.Response{Message: "服务器内部错误"})
},
})
)
func init() {
2021-12-14 13:53:22 +08:00
//GetConfig
bgm.GetConfig()
2021-12-13 21:39:15 +08:00
handlerMap[createCategory] = handler.CreateCategoryH
//requestId
app.Use(requestid.New())
//logger
app.Use(logger.New(logger.Config{
Format: "${pid} ${locals:requestid} ${status} - ${method} ${path} ${resBody}\n",
}))
//recover
app.Use(recover.New(recover.Config{
EnableStackTrace: true,
StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
buf := make([]byte, 1024*1024)
buf = buf[:runtime.Stack(buf, false)]
2021-12-14 14:31:18 +08:00
ctxLogger.FError(c, consts.PanicError, zap.String(consts.ErrorReason, fmt.Sprintf("panic: %v\n%s\n", e, buf)))
2021-12-13 21:39:15 +08:00
},
}))
2021-12-14 14:31:18 +08:00
mysqlMap, err = bgm.GormMap(bgm.ConfigMap()[bgm.GetConfigKey(consts.DsnPre)])
2021-12-13 21:39:15 +08:00
if err != nil {
2021-12-14 14:31:18 +08:00
ctxLogger.FError(nil, consts.GormError, zap.String(consts.ErrorReason, err.Error()))
2021-12-13 21:39:15 +08:00
os.Exit(-1)
}
2021-12-14 14:31:18 +08:00
routerInit()
2021-12-13 21:39:15 +08:00
}
2021-12-14 14:31:18 +08:00
func routerInit() {
handlerMap[createCategory] = handler.CreateCategoryH
}
func serverStart() {
port := bgm.GetEnvDefault(consts.Port, consts.PortDefault)
err = app.Listen(fmt.Sprintf(":%v", port))
2021-12-13 21:39:15 +08:00
if err != nil {
ctxLogger.Error(nil, "", zap.String("err", err.Error()))
os.Exit(-1)
}
}
2021-12-14 14:31:18 +08:00
func main() {
app.Get("/*", func(ctx *fiber.Ctx) error {
return handlerMap[ctx.Path()](ctx, mysqlMap)
})
serverStart()
2021-12-14 14:31:18 +08:00
}