package main import ( "allapp/api/router" "allapp/api/router/middleware" "allapp/config" "allapp/utils/responsex" "fmt" "runtime/debug" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/log" recover2 "github.com/gofiber/fiber/v3/middleware/recover" "github.com/gofiber/fiber/v3/middleware/requestid" "go.uber.org/zap" ) type structValidator struct { validate *validator.Validate } // Validate Validator needs to implement the Validate method func (v *structValidator) Validate(out any) error { return v.validate.Struct(out) } func main() { if err := config.InitConfig(); err != nil { panic(err) } app := fiber.New(fiber.Config{ StructValidator: &structValidator{validate: validator.New()}, ErrorHandler: func(ctx fiber.Ctx, err error) error { log.WithContext(ctx.Context()).Errorw("系统异常", "method", ctx.Method(), "path", ctx.Path(), "status", ctx.Response().StatusCode(), "error", err, ) return responsex.SystemError(ctx, fmt.Sprintf("系统异常 - %v", err)) }, }) app.Use(recover2.New(recover2.Config{ EnableStackTrace: true, StackTraceHandler: func(ctx fiber.Ctx, err any) { stack := debug.Stack() // 获取 panic 堆栈 // 打日志到 zap log.WithContext(ctx.Context()).Errorw("系统堆栈异常", "error", err, "stack", string(stack), ) }, }), requestid.New(), middleware.Logger()) router.SetupRouters(app) if err := app.Listen(config.GetServerPort()); err != nil { log.Fatal("server start failed", zap.Error(err)) } }