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

129 lines
3.2 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/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"
2022-01-13 14:28:50 +08:00
"git.icechen.cn/monorepo/backend/pkg/api"
2022-01-12 14:34:31 +08:00
"github.com/gofiber/fiber/v2"
"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
//Init App
app = fiber.New(fiber.Config{
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
return ctx.Status(fiber.StatusInternalServerError).JSON(handler.Response{Message: "服务器内部错误"})
},
})
)
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() {
//requestId
app.Use(requestid.New())
//recover
2022-01-13 14:48:28 +08:00
app.Use(api.FibberRecoverHandler)
2022-01-12 14:34:31 +08:00
//requestId for gorm
2022-01-13 14:48:28 +08:00
app.Use(api.FibberUserCtxWithTraceHandler)
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)
})
}
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)
}
}
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
}