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:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package wire
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"carrot_bbs/internal/cache"
|
||||
@@ -62,7 +61,9 @@ func ProvideDB(cfg *config.Config) (*gorm.DB, error) {
|
||||
func ProvideRedisClient(cfg *config.Config) *redis.Client {
|
||||
client, err := redis.New(&cfg.Redis)
|
||||
if err != nil {
|
||||
log.Printf("[WARNING] Failed to connect to Redis: %v, falling back to memory cache", err)
|
||||
zap.L().Warn("Failed to connect to Redis, falling back to memory cache",
|
||||
zap.Error(err),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
return client
|
||||
@@ -85,12 +86,12 @@ func ProvideCache(cfg *config.Config, redisClient *redis.Client) cache.Cache {
|
||||
|
||||
// 直接创建 RedisCache 实例,不依赖全局变量
|
||||
if redisClient == nil {
|
||||
log.Println("[Wire] Warning: Redis client is nil, cache will not be available")
|
||||
zap.L().Warn("Redis client is nil, cache will not be available")
|
||||
return nil
|
||||
}
|
||||
|
||||
cacheInstance := cache.NewRedisCache(redisClient)
|
||||
log.Println("[Wire] Initialized Redis cache via dependency injection")
|
||||
zap.L().Info("Initialized Redis cache via dependency injection")
|
||||
return cacheInstance
|
||||
}
|
||||
|
||||
@@ -128,7 +129,9 @@ func ProvideTransactionManager(db *gorm.DB) repository.TransactionManager {
|
||||
func ProvideLogger() *zap.Logger {
|
||||
logger, err := zap.NewProduction()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create logger: %v", err)
|
||||
panic("Failed to create logger: " + err.Error())
|
||||
}
|
||||
// 设置为全局 logger,以便在其他地方使用 zap.L()
|
||||
zap.ReplaceGlobals(logger)
|
||||
return logger
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user