37 lines
1.2 KiB
Go
37 lines
1.2 KiB
Go
package svc
|
|
|
|
import (
|
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"tianyuan-api/apps/sentinel/internal/config"
|
|
"tianyuan-api/apps/sentinel/internal/model"
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
Redis *redis.Redis
|
|
WhitelistModel model.WhitelistModel
|
|
SecretsModel model.SecretsModel
|
|
ProductsModel model.ProductsModel
|
|
UserProductsModel model.UserProductsModel
|
|
}
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
db := sqlx.NewMysql(c.DataSource) // 创建数据库连接
|
|
redisConf := redis.RedisConf{
|
|
Host: c.CacheRedis[0].Host,
|
|
Pass: c.CacheRedis[0].Pass,
|
|
Type: c.CacheRedis[0].Type, // Redis 节点类型,如 "node"
|
|
}
|
|
// 使用 MustNewRedis 来初始化 Redis 客户端
|
|
rds := redis.MustNewRedis(redisConf)
|
|
return &ServiceContext{
|
|
Config: c,
|
|
Redis: rds,
|
|
WhitelistModel: model.NewWhitelistModel(rds, db, c.CacheRedis),
|
|
SecretsModel: model.NewSecretsModel(db, c.CacheRedis),
|
|
ProductsModel: model.NewProductsModel(db, c.CacheRedis),
|
|
UserProductsModel: model.NewUserProductsModel(rds, db, c.CacheRedis),
|
|
}
|
|
}
|