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

147 lines
3.9 KiB
Go
Executable File

package main
import (
"context"
"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"
"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 (
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())
//logger
app.Use(logger.New(logger.Config{
Format: "${respHeader:X-Request-ID} ${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)]
fmt.Printf("panic: %v\n%s\n", e, buf)
ctxLogger.FError(c, error_process.PanicError, zap.String("", fmt.Sprintf("panic: %v\n%s\n", e, buf)))
},
}))
//requestId for gorm
app.Use(func(ctx *fiber.Ctx) error {
ctx.SetUserContext(context.WithValue(context.Background(), fiber.HeaderXRequestID, ctx.Response().Header.Peek(fiber.HeaderXRequestID)))
return ctx.Next()
})
}
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()
}