首次提交:初始化项目代码

This commit is contained in:
sunct
2026-07-31 14:44:48 +08:00
commit a4fe393571
119 changed files with 29105 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
// Package middleware Gin 中间件层,提供请求日志、认证等横切能力
package middleware
import (
"time"
"resume-platform/pkg/logger"
"github.com/gin-gonic/gin"
)
func RequestLogger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
method := c.Request.Method
c.Next()
latency := time.Since(start)
statusCode := c.Writer.Status()
clientIP := c.ClientIP()
referer := c.Request.Referer()
userAgent := c.Request.UserAgent()
entry := logger.FromContext(c.Request.Context())
entry = entry.WithFields(map[string]interface{}{
"status": statusCode,
"latency": latency,
"ip": clientIP,
"method": method,
"path": path,
})
if referer != "" {
entry = entry.WithField("referer", referer)
}
if len(c.Errors) > 0 {
entry = entry.WithField("errors", c.Errors.String())
}
switch {
case statusCode >= 500:
entry.WithField("user_agent", userAgent).Error("Server error")
case statusCode >= 400:
entry.Warn("Client error")
default:
entry.Info("Request handled")
}
}
}
+25
View File
@@ -0,0 +1,25 @@
package middleware
import (
"context"
"resume-platform/pkg/constant"
"resume-platform/pkg/idgen"
"github.com/gin-gonic/gin"
)
func Tracing(appName string) gin.HandlerFunc {
return func(c *gin.Context) {
reqId, _ := idgen.GetID()
ctx := context.WithValue(c.Request.Context(), constant.RequestIdKey, reqId)
ctx = context.WithValue(ctx, constant.AppNameKey, appName)
ctx = context.WithValue(ctx, constant.RequestRouteKey, c.Request.URL.Path)
ctx = context.WithValue(ctx, constant.ClientIP, c.ClientIP())
c.Request = c.Request.WithContext(ctx)
c.Next()
}
}