package svc import ( "backend/usercenter/api/internal/config" "fmt" "github.com/redis/go-redis/v9" "gorm.io/driver/mysql" "gorm.io/gorm" ) type ServiceContext struct { Config config.Config Db *gorm.DB RedisClient *redis.Client } func NewServiceContext(c config.Config) *ServiceContext { dns := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", c.MySQL.User, c.MySQL.Password, c.MySQL.Host, c.MySQL.Port, c.MySQL.DBName) db, err := gorm.Open(mysql.Open(dns), &gorm.Config{}) if err != nil { panic(err) } return &ServiceContext{ Config: c, Db: db, RedisClient: redis.NewClient(&redis.Options{ Addr: fmt.Sprintf("%s:%d", c.Redis.Host, c.Redis.Port), Password: c.Redis.Pass, DB: c.Redis.TkDB, }), } }