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.
33 lines
788 B
33 lines
788 B
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,
|
|
}),
|
|
}
|
|
}
|
|
|