feat(backend): Go 服务骨架(配置、日志、数据库、中间件、Wire)
- config: Viper YAML 配置 + 环境变量覆盖 - logs: zap 结构化日志 + trace_id 链路追踪 + 敏感信息脱敏 - db: PostgreSQL (GORM) + Redis (go-redis) 连接管理 - middleware: Trace/Logger/CORS/Recovery 四层中间件 - provider: Wire 编译时依赖注入 - main.go: 优雅启动/关闭 HTTP 服务 Made-with: Cursor
This commit is contained in:
46
backend/go-service/app/provider/provider.go
Normal file
46
backend/go-service/app/provider/provider.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Package provider 提供全局依赖注入配置
|
||||
// 使用 Wire 编译时依赖注入,集中管理所有模块的 Provider Set
|
||||
package provider
|
||||
|
||||
import (
|
||||
"github.com/echochat/backend/config"
|
||||
"github.com/echochat/backend/pkg/db"
|
||||
"github.com/google/wire"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// App 应用根容器,持有所有基础设施组件
|
||||
type App struct {
|
||||
Config *config.Config
|
||||
DB *gorm.DB
|
||||
Redis *redis.Client
|
||||
}
|
||||
|
||||
// NewApp 创建应用实例
|
||||
func NewApp(cfg *config.Config, gormDB *gorm.DB, redisClient *redis.Client) *App {
|
||||
return &App{
|
||||
Config: cfg,
|
||||
DB: gormDB,
|
||||
Redis: redisClient,
|
||||
}
|
||||
}
|
||||
|
||||
// provideDBConfig 从全局 Config 中提取 DatabaseConfig
|
||||
func provideDBConfig(cfg *config.Config) *config.DatabaseConfig {
|
||||
return &cfg.Database
|
||||
}
|
||||
|
||||
// provideRedisConfig 从全局 Config 中提取 RedisConfig
|
||||
func provideRedisConfig(cfg *config.Config) *config.RedisConfig {
|
||||
return &cfg.Redis
|
||||
}
|
||||
|
||||
// InfraSet 基础设施层 Provider Set
|
||||
var InfraSet = wire.NewSet(
|
||||
provideDBConfig,
|
||||
provideRedisConfig,
|
||||
db.NewPostgres,
|
||||
db.NewRedis,
|
||||
NewApp,
|
||||
)
|
||||
Reference in New Issue
Block a user