feat: implement centralized error response handling and add logger middleware

This commit is contained in:
Laisky.Cai
2025-02-26 00:52:24 +00:00
parent 607406dae1
commit db992c26c4
5 changed files with 595 additions and 111 deletions

16
main.go
View File

@@ -7,6 +7,8 @@ import (
"os"
"strconv"
gmw "github.com/Laisky/gin-middlewares/v6"
glog "github.com/Laisky/go-utils/v5/log"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
@@ -101,8 +103,20 @@ func main() {
}
// Initialize HTTP server
logLevel := glog.LevelInfo
if config.DebugEnabled {
logLevel = glog.LevelDebug
}
server := gin.New()
server.Use(gin.Recovery())
server.RedirectTrailingSlash = false
server.Use(
gin.Recovery(),
gmw.NewLoggerMiddleware(
gmw.WithLoggerMwColored(),
gmw.WithLevel(logLevel.String()),
gmw.WithLogger(glog.Shared.Named("one-api")),
),
)
// This will cause SSE not to work!!!
//server.Use(gzip.Gzip(gzip.DefaultCompression))
server.Use(middleware.RequestId())