数据结构优化

This commit is contained in:
李琦
2026-01-20 14:31:39 +08:00
parent 05b315013c
commit 946855bff2
16 changed files with 104045 additions and 157 deletions

View File

@@ -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)
}
}()
}