forked from free-lancers/electricity_bill_calc_service
refactor(app):基本完成基础服务框架的功能构建。
This commit is contained in:
@@ -3,52 +3,65 @@ package router
|
||||
import (
|
||||
"electricity_bill_calc/controller"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"time"
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
ginzap "github.com/gin-contrib/zap"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/compress"
|
||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func Router() *gin.Engine {
|
||||
router := gin.New()
|
||||
router.Use(ginzap.Ginzap(logger.GetLogger(), time.RFC3339, false))
|
||||
router.Use(ginzap.RecoveryWithZap(logger.GetLogger(), true))
|
||||
router.Use(security.SessionRecovery)
|
||||
func App() *fiber.App {
|
||||
app := fiber.New(fiber.Config{
|
||||
BodyLimit: 10 * 1024 * 1024,
|
||||
EnablePrintRoutes: true,
|
||||
EnableTrustedProxyCheck: false,
|
||||
Prefork: false,
|
||||
ErrorHandler: errorHandler,
|
||||
})
|
||||
app.Use(compress.New())
|
||||
app.Use(recover.New(recover.Config{
|
||||
EnableStackTrace: true,
|
||||
StackTraceHandler: stackTraceHandler,
|
||||
}))
|
||||
app.Use(logger.NewLogMiddleware(logger.LogMiddlewareConfig{
|
||||
Logger: logger.Named("App"),
|
||||
}))
|
||||
app.Use(security.SessionRecovery)
|
||||
|
||||
controller.InitializeUserController(router)
|
||||
controller.InitializeRegionController(router)
|
||||
controller.InitializeChargesController(router)
|
||||
controller.InitializeParkController(router)
|
||||
controller.InitializeMaintenanceFeeController(router)
|
||||
controller.InitializeMeter04kVController(router)
|
||||
controller.InitializeReportController(router)
|
||||
controller.InitializeEndUserController(router)
|
||||
controller.InitializeWithdrawController(router)
|
||||
controller.InitializeStatisticsController(router)
|
||||
controller.InitializeGodModeController(router)
|
||||
controller.InitializeUserController(app)
|
||||
controller.InitializeRegionController(app)
|
||||
controller.InitializeChargesController(app)
|
||||
controller.InitializeParkController(app)
|
||||
controller.InitializeMaintenanceFeeController(app)
|
||||
controller.InitializeMeter04kVController(app)
|
||||
controller.InitializeReportController(app)
|
||||
controller.InitializeEndUserController(app)
|
||||
controller.InitializeWithdrawController(app)
|
||||
controller.InitializeStatisticsController(app)
|
||||
controller.InitializeGodModeController(app)
|
||||
|
||||
return router
|
||||
return app
|
||||
}
|
||||
|
||||
// 404
|
||||
func HandleNotFound(c *gin.Context) {
|
||||
response.NewResult(c).NotFound("指定资源未找到。")
|
||||
// 全局错误处理
|
||||
func errorHandler(c *fiber.Ctx, err error) error {
|
||||
code := fiber.StatusInternalServerError
|
||||
if e, ok := err.(*fiber.Error); ok {
|
||||
code = e.Code
|
||||
}
|
||||
e := c.Status(code).SendString(err.Error())
|
||||
if e != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 500
|
||||
func Recover(c *gin.Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
//打印错误堆栈信息
|
||||
if err, ok := r.(error); ok {
|
||||
logger.Error(err.Error(), zap.Error(err))
|
||||
}
|
||||
// response.NewResult(c).Error(500, "服务器内部错误")
|
||||
}
|
||||
}()
|
||||
//继续后续接口调用
|
||||
c.Next()
|
||||
// 处理Recover中间件输出的栈追踪信息
|
||||
func stackTraceHandler(c *fiber.Ctx, e interface{}) {
|
||||
buf := make([]byte, 1024)
|
||||
buf = buf[:runtime.Stack(buf, false)]
|
||||
logger.Named("App", "StackTrace").Warn(fmt.Sprintf("panic: %+v", e), zap.ByteString("trace", buf), zap.Any("origin", e))
|
||||
}
|
||||
|
Reference in New Issue
Block a user