package main import ( "bgm/consts" "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" ) type handleM map[string]func(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error var ( mysqlMap = make(bgm.MysqlMap, 0) err error handlerMap = make(handleM, 0) app = fiber.New(fiber.Config{ ErrorHandler: func(ctx *fiber.Ctx, err error) error { return ctx.Status(fiber.StatusInternalServerError).JSON(model.Response{Message: "服务器内部错误"}) }, }) ) func init() { //GetConfig bgm.GetConfig() 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)] ctxLogger.FError(c, consts.PanicError, zap.String(consts.ErrorReason, fmt.Sprintf("panic: %v\n%s\n", e, buf))) }, })) mysqlMap, err = bgm.GormMap(bgm.ConfigMap()[bgm.GetConfigKey(consts.DsnPre)]) if err != nil { ctxLogger.FError(nil, consts.GormError, zap.String(consts.ErrorReason, err.Error())) os.Exit(-1) } routerInit() } func routerInit() { handlerMap[createCategory] = handler.CreateCategoryH } func serverStart() { port := bgm.GetEnvDefault(consts.Port, consts.PortDefault) err = app.Listen(fmt.Sprintf(":%v", port)) if err != nil { ctxLogger.Error(nil, "", zap.String("err", err.Error())) os.Exit(-1) } } func main() { app.Get("/*", func(ctx *fiber.Ctx) error { return handlerMap[ctx.Path()](ctx, mysqlMap) }) serverStart() }