package main import ( "fmt" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/gorm_helper" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/handler" "git.icechen.cn/monorepo/backend/pkg/api" "git.icechen.cn/monorepo/backend/pkg/config" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/recover" "github.com/gofiber/fiber/v2/middleware/requestid" ctxLogger "github.com/luizsuper/ctxLoggers" "go.uber.org/zap" "os" ) const ( tag = "/tag" category = "/category" scripts = "/scripts" script = "/script" userInfo = "/userInfo" journey = "/js" ) var ( err error app = fiber.New(fiber.Config{ ErrorHandler: api.FiberMiddleWareErrHandler, }) ) func init() { ResourceInit() appInit() routerInit() } // ResourceInit 读取配置初始化资源 func ResourceInit() { mysql, err := config.GetMysql() if err != nil { ctxLogger.FError(nil, "etcd读取mysql出错", zap.String("", err.Error())) os.Exit(-1) } err = gorm_helper.GormMap(mysql) if err != nil { ctxLogger.FError(nil, error_process.GormError, zap.String("", err.Error())) os.Exit(-1) } } func appInit() { app.Use(requestid.New()) app.Use(api.FibberUserCtxWithTraceHandler) app.Use(api.FibberMiddleWareRespHandler) app.Use(recover.New(api.GetFibberRecoverConfig())) } func routerInit() { //Tag router tagRoute := app.Group(tag) tagRoute.Get("", func(ctx *fiber.Ctx) error { return handler.GetTagH(ctx) }) tagRoute.Post("", func(ctx *fiber.Ctx) error { return handler.CreateTagH(ctx) }) tagRoute.Put("", func(ctx *fiber.Ctx) error { return handler.UpdateTagH(ctx) }) tagRoute.Delete("", func(ctx *fiber.Ctx) error { return handler.DeleteTagH(ctx) }) tagRoute.Post("/categories", func(ctx *fiber.Ctx) error { return handler.CreateCategoryForTag(ctx) }) tagRoute.Delete("/categories", func(ctx *fiber.Ctx) error { return handler.DeleteCategoryForTag(ctx) }) //Category router categoryRoute := app.Group(category) categoryRoute.Get("", func(ctx *fiber.Ctx) error { return handler.GetCategoryH(ctx) }) categoryRoute.Post("", func(ctx *fiber.Ctx) error { return handler.CreateCategoryH(ctx) }) categoryRoute.Put("", func(ctx *fiber.Ctx) error { return handler.UpdateCategoryH(ctx) }) categoryRoute.Delete("", func(ctx *fiber.Ctx) error { return handler.DeleteCategoryH(ctx) }) categoryRoute.Post("/tags", func(ctx *fiber.Ctx) error { return handler.CreateTagForCategory(ctx) }) categoryRoute.Delete("/tags", func(ctx *fiber.Ctx) error { return handler.DeleteTagForCategory(ctx) }) scriptsGroup := app.Group(scripts) scriptsGroup.Get("", func(ctx *fiber.Ctx) error { return handler.GetScriptsH(ctx) }) scriptsGroup.Put("", func(ctx *fiber.Ctx) error { return handler.UpdateScriptsH(ctx) }) scriptsGroup.Post("", func(ctx *fiber.Ctx) error { return handler.CreateScriptsH(ctx) }) scriptsGroup.Delete("", func(ctx *fiber.Ctx) error { return handler.DeleteScriptsH(ctx) }) scriptGroup := app.Group(script) scriptGroup.Get("", func(ctx *fiber.Ctx) error { return handler.GetScriptApi(ctx) }) userGroup := app.Group(userInfo) userGroup.Get("login", func(ctx *fiber.Ctx) error { return handler.Login(ctx) }) journeyGroup := app.Group(journey) journeyGroup.Get("", func(ctx *fiber.Ctx) error { return handler.GetJourneyH(ctx) }) } func serverStart() { err = app.Listen(fmt.Sprintf(":%v", 8080)) if err != nil { ctxLogger.Error(nil, "", zap.String("", err.Error())) os.Exit(-1) } } func main() { serverStart() }