数据结构优化
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -12,6 +13,13 @@ import (
|
||||
// AccessLogMiddleware 访问日志中间件 (用于流量统计)
|
||||
func AccessLogMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 添加 panic recover 保护整个中间件
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic in AccessLogMiddleware: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
start := time.Now()
|
||||
|
||||
c.Next()
|
||||
@@ -24,9 +32,27 @@ func AccessLogMiddleware() gin.HandlerFunc {
|
||||
ip := c.ClientIP()
|
||||
|
||||
// 获取归属地 (需要先初始化ip2region)
|
||||
region := utils.GetRegion(ip)
|
||||
// 使用 recover 保护,确保即使 GetRegion 失败也能继续记录日志
|
||||
var region string
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic in GetRegion for IP %s (access log): %v", ip, r)
|
||||
region = "Unknown"
|
||||
}
|
||||
}()
|
||||
region = utils.GetRegion(ip)
|
||||
}()
|
||||
|
||||
log := &models.AccessLog{
|
||||
// 如果 region 为空,设置为 Unknown
|
||||
if region == "" {
|
||||
region = "Unknown"
|
||||
}
|
||||
|
||||
// 记录调试信息
|
||||
log.Printf("Creating access log: IP=%s, Region=%s, Path=%s, Method=%s", ip, region, c.Request.URL.Path, c.Request.Method)
|
||||
|
||||
logEntry := &models.AccessLog{
|
||||
IP: ip,
|
||||
UserAgent: c.Request.UserAgent(),
|
||||
Path: c.Request.URL.Path,
|
||||
@@ -36,9 +62,19 @@ func AccessLogMiddleware() gin.HandlerFunc {
|
||||
Region: region,
|
||||
}
|
||||
|
||||
// 异步入库
|
||||
// 异步入库,添加错误处理和 panic 保护
|
||||
go func() {
|
||||
repositories.CreateAccessLog(log)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic in CreateAccessLog goroutine: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := repositories.CreateAccessLog(logEntry); err != nil {
|
||||
log.Printf("Error creating access log for IP=%s, Region=%s, Path=%s: %v", ip, region, c.Request.URL.Path, err)
|
||||
} else {
|
||||
log.Printf("Successfully created access log: IP=%s, Region=%s, Path=%s", ip, region, c.Request.URL.Path)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,25 @@ func OperationLogMiddleware() gin.HandlerFunc {
|
||||
|
||||
// 获取IP归属地
|
||||
ip := c.ClientIP()
|
||||
region := utils.GetRegion(ip)
|
||||
var region string
|
||||
// 使用 recover 保护 GetRegion 调用
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic in GetRegion for IP %s (operation log): %v", ip, r)
|
||||
region = "Unknown"
|
||||
}
|
||||
}()
|
||||
region = utils.GetRegion(ip)
|
||||
}()
|
||||
|
||||
// 确保 region 不为空,如果为空则设置为 "Unknown"
|
||||
if region == "" {
|
||||
region = "Unknown"
|
||||
}
|
||||
|
||||
// 记录调试信息
|
||||
log.Printf("Creating operation log: UserID=%d, IP=%s, Region=%s, Path=%s", userID.(uint), ip, region, c.Request.URL.Path)
|
||||
|
||||
// 构建操作日志
|
||||
operationLog := &models.OperationLog{
|
||||
@@ -66,8 +84,16 @@ func OperationLogMiddleware() gin.HandlerFunc {
|
||||
|
||||
// 异步记录日志,避免影响响应
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic in CreateOperationLog goroutine: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := repositories.CreateOperationLog(operationLog); err != nil {
|
||||
log.Printf("Error creating operation log: %v", err)
|
||||
log.Printf("Error creating operation log for UserID=%d, IP=%s, Region=%s: %v", userID.(uint), ip, region, err)
|
||||
} else {
|
||||
log.Printf("Successfully created operation log: UserID=%d, IP=%s, Region=%s", userID.(uint), ip, region)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user