refactor: cleanup unused code and simplify internal packages
This commit performs a significant cleanup of the codebase by removing unused functions, methods, and entire files across various internal modules. This reduces technical debt and simplifies the project structure. Key changes include: - **cache**: Removed unused cache key generators and metrics snapshots. - **dto**: Removed redundant converter functions and segment creation helpers. - **middleware**: Deleted the unused `logger.go` middleware and simplified `ratelimit.go` and `casbin.go`. - **model**: Removed unused ID helpers, database closing functions, and batch decryption logic. - **pkg**: Cleaned up unused utility functions in `circuitbreaker`, `crypto`, `cursor`, `hook`, and `utils`. - **service**: Deleted `account_cleanup_service.go` and removed unused helper functions in `log_cleanup_service.go` and `sensitive_service.go`. - **repository**: Removed unused private loading methods in `comment_repo.go`.
This commit is contained in:
@@ -66,23 +66,6 @@ func CasbinAuth(casbinService service.CasbinService) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// OptionalCasbinAuth 可选的 Casbin 权限检查
|
||||
// 不阻止请求,但会设置权限标志
|
||||
func OptionalCasbinAuth(casbinService service.CasbinService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
userID, exists := c.Get("user_id")
|
||||
if exists {
|
||||
path := c.Request.URL.Path
|
||||
method := c.Request.Method
|
||||
allowed, err := casbinService.EnforceForUser(c.Request.Context(), userID.(string), path, method)
|
||||
if err == nil {
|
||||
c.Set("permission_granted", allowed)
|
||||
}
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// RequireRole 要求特定角色的中间件
|
||||
func RequireRole(casbinService service.CasbinService, requiredRoles ...string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Logger 日志中间件
|
||||
func Logger(logger *zap.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
|
||||
c.Next()
|
||||
|
||||
latency := time.Since(start)
|
||||
statusCode := c.Writer.Status()
|
||||
|
||||
logger.Info("request",
|
||||
zap.String("method", c.Request.Method),
|
||||
zap.String("path", path),
|
||||
zap.Int("status", statusCode),
|
||||
zap.Duration("latency", latency),
|
||||
zap.String("ip", c.ClientIP()),
|
||||
zap.String("user-agent", c.Request.UserAgent()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Recovery 恢复中间件
|
||||
func Recovery(logger *zap.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
logger.Error("panic recovered",
|
||||
zap.Any("error", err),
|
||||
)
|
||||
c.JSON(500, gin.H{
|
||||
"code": 500,
|
||||
"message": "internal server error",
|
||||
})
|
||||
c.Abort()
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -75,27 +75,6 @@ func (rl *RateLimiter) isAllowed(key string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (rl *RateLimiter) count(key string) int {
|
||||
rl.mu.Lock()
|
||||
defer rl.mu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
times := rl.requests[key]
|
||||
|
||||
var valid []time.Time
|
||||
for _, t := range times {
|
||||
if now.Sub(t) < rl.window {
|
||||
valid = append(valid, t)
|
||||
}
|
||||
}
|
||||
rl.requests[key] = valid
|
||||
return len(valid)
|
||||
}
|
||||
|
||||
func RateLimit(requestsPerMinute int) gin.HandlerFunc {
|
||||
return RateLimitWithDuration(requestsPerMinute, time.Minute)
|
||||
}
|
||||
|
||||
func RateLimitWithDuration(limit int, window time.Duration) gin.HandlerFunc {
|
||||
limiter := NewRateLimiter(limit, window)
|
||||
|
||||
@@ -115,32 +94,6 @@ func RateLimitWithDuration(limit int, window time.Duration) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func RateLimitWithKey(requestsPerMinute int, keyFunc func(c *gin.Context) string) gin.HandlerFunc {
|
||||
return RateLimitWithDurationAndKey(requestsPerMinute, time.Minute, keyFunc)
|
||||
}
|
||||
|
||||
func RateLimitWithDurationAndKey(limit int, window time.Duration, keyFunc func(c *gin.Context) string) gin.HandlerFunc {
|
||||
limiter := NewRateLimiter(limit, window)
|
||||
|
||||
return func(c *gin.Context) {
|
||||
key := keyFunc(c)
|
||||
if key == "" {
|
||||
key = c.ClientIP()
|
||||
}
|
||||
|
||||
if !limiter.isAllowed(key) {
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{
|
||||
"code": 429,
|
||||
"message": "too many requests, please try again later",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// IP 封禁与登录失败追踪
|
||||
// ============================================================
|
||||
|
||||
Reference in New Issue
Block a user