refactor: replace standard log with zap logger and optimize code patterns

- Migrate all log.Printf/log.Println calls to structured zap logging
- Use cmp.Or for cleaner default value handling
- Replace manual loops with slices.ContainsFunc/IndexFunc
- Add batch query methods (IsLikedBatch, IsFavoritedBatch) to solve N+1 problem
- Update JSON tags from omitempty to omitzero for numeric fields
- Use errgroup-style wg.Go for worker goroutines
- Remove duplicate casbin/v2 dependency (keeping v3)
- Add plans/ to gitignore
This commit is contained in:
lafay
2026-03-17 00:47:17 +08:00
parent b028f7e1d3
commit 5d6c982c9c
38 changed files with 2256 additions and 290 deletions

View File

@@ -1,8 +1,6 @@
package wire
import (
"log"
"carrot_bbs/internal/cache"
"carrot_bbs/internal/config"
"carrot_bbs/internal/repository"
@@ -11,6 +9,7 @@ import (
"github.com/casbin/casbin/v3"
gormadapter "github.com/casbin/gorm-adapter/v3"
"github.com/google/wire"
"go.uber.org/zap"
"gorm.io/gorm"
)
@@ -26,21 +25,27 @@ func ProvideCasbinEnforcer(cfg *config.Config, db *gorm.DB) *casbin.Enforcer {
// 创建 GORM 适配器,连接数据库
adapter, err := gormadapter.NewAdapterByDB(db)
if err != nil {
log.Fatalf("Failed to create Casbin GORM adapter: %v", err)
zap.L().Fatal("Failed to create Casbin GORM adapter",
zap.Error(err),
)
}
// 创建 Casbin Enforcer使用数据库适配器
enforcer, err := casbin.NewEnforcer(cfg.Casbin.ModelPath, adapter)
if err != nil {
log.Fatalf("Failed to create Casbin enforcer: %v", err)
zap.L().Fatal("Failed to create Casbin enforcer",
zap.Error(err),
)
}
// 从数据库加载策略
if err := enforcer.LoadPolicy(); err != nil {
log.Printf("[WARNING] Failed to load Casbin policy: %v", err)
zap.L().Warn("Failed to load Casbin policy",
zap.Error(err),
)
}
log.Println("[Wire] Initialized Casbin enforcer with GORM adapter")
zap.L().Info("Initialized Casbin enforcer with GORM adapter")
return enforcer
}