backend/app/brahma/api/murder/main.go

136 lines
3.4 KiB
Go
Raw Normal View History

2022-01-11 13:18:59 +08:00
package main
2022-01-12 14:34:31 +08:00
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"
2022-01-13 14:28:50 +08:00
"git.icechen.cn/monorepo/backend/pkg/api"
"git.icechen.cn/monorepo/backend/pkg/config"
2022-01-12 14:34:31 +08:00
"github.com/gofiber/fiber/v2"
2022-01-14 04:09:44 +08:00
"github.com/gofiber/fiber/v2/middleware/recover"
2022-01-12 14:34:31 +08:00
"github.com/gofiber/fiber/v2/middleware/requestid"
ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap"
"os"
)
const (
tag = "/tag"
category = "/category"
scripts = "/scripts"
2022-01-19 19:39:04 +08:00
script = "/script"
2022-01-25 17:03:14 +08:00
userInfo = "/userInfo"
2022-01-12 14:34:31 +08:00
)
var (
err error
app = fiber.New(fiber.Config{
2022-01-14 04:09:44 +08:00
ErrorHandler: api.FiberMiddleWareErrHandler,
2022-01-12 14:34:31 +08:00
})
)
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)
2022-01-12 14:34:31 +08:00
if err != nil {
ctxLogger.FError(nil, error_process.GormError, zap.String("", err.Error()))
os.Exit(-1)
}
}
func appInit() {
app.Use(requestid.New())
2022-01-13 14:48:28 +08:00
app.Use(api.FibberUserCtxWithTraceHandler)
2022-01-14 04:09:44 +08:00
app.Use(api.FibberMiddleWareRespHandler)
app.Use(recover.New(api.GetFibberRecoverConfig()))
2022-01-12 14:34:31 +08:00
}
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)
})
2022-01-19 19:39:04 +08:00
scriptGroup := app.Group(script)
scriptGroup.Get("", func(ctx *fiber.Ctx) error {
return handler.GetScriptApi(ctx)
})
2022-01-25 17:03:14 +08:00
userGroup := app.Group(userInfo)
userGroup.Get("login", func(ctx *fiber.Ctx) error {
return handler.Login(ctx)
})
2022-01-12 14:34:31 +08:00
}
func serverStart() {
err = app.Listen(fmt.Sprintf(":%v", 8080))
2022-01-12 14:34:31 +08:00
if err != nil {
ctxLogger.Error(nil, "", zap.String("", err.Error()))
os.Exit(-1)
}
}
2022-01-11 13:18:59 +08:00
func main() {
2022-01-12 14:34:31 +08:00
serverStart()
2022-01-11 13:18:59 +08:00
}