You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

39 lines
844 B

package svc
import (
// 引入配置文件
"backend/usercenter/rpc/internal/config"
// 引入GORM
"gorm.io/driver/mysql"
"gorm.io/gorm"
// 引入redis
"fmt"
"github.com/redis/go-redis/v9"
)
// 服务上下文
type ServiceContext struct {
Config config.Config
UsercenterDB *gorm.DB
RedisClient *redis.Client // 注意类型
}
func NewServiceContext(c config.Config) *ServiceContext {
usercenterDB, err := gorm.Open(mysql.Open(c.MySQL.DSN()), &gorm.Config{})
if err != nil {
panic(err)
}
redisClient := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", c.RedisGo.Host, c.RedisGo.Port),
Password: c.RedisGo.Pass, // 或 Password,取决于你的 config.go/yaml
DB: c.RedisGo.DB,
})
return &ServiceContext{
Config: c,
UsercenterDB: usercenterDB,
RedisClient: redisClient,
}
}