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

125 lines
3.1 KiB
Go
Executable File

package main
import (
"fmt"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/config"
"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"
"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"
)
var (
err error
app = fiber.New(fiber.Config{
ErrorHandler: api.FiberMiddleWareErrHandler,
})
)
func init() {
ResourceInit()
appInit()
routerInit()
}
// ResourceInit 读取配置初始化资源
func ResourceInit() {
config.GetConfig()
err = gorm_helper.GormMap(config.GetConfigMap()[config.GetConfigKey(config.DsnPre)])
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)
})
}
func serverStart() {
port := config.GetEnvDefault(config.Port, config.PortDefault)
err = app.Listen(fmt.Sprintf(":%v", port))
if err != nil {
ctxLogger.Error(nil, "", zap.String("", err.Error()))
os.Exit(-1)
}
}
func main() {
serverStart()
}