You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package svc
|
|
|
|
import (
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
"live-service/app/user_center/model"
|
|
"live-service/app/user_center/rpc/internal/config"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
|
|
UserModel model.UserModel
|
|
UserPlatformModel model.UserPlatformModel
|
|
StatisticsPvpModel model.StatisticsPvpModel
|
|
RankPvpModel model.RankPvpModel
|
|
}
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
gormDb, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{
|
|
Logger: logger.New(
|
|
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
|
logger.Config{
|
|
SlowThreshold: 2 * time.Second,
|
|
LogLevel: logger.Warn,
|
|
IgnoreRecordNotFoundError: true,
|
|
Colorful: true,
|
|
},
|
|
),
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return &ServiceContext{
|
|
Config: c,
|
|
UserModel: model.NewUserModel(gormDb),
|
|
UserPlatformModel: model.NewUserPlatformModel(gormDb),
|
|
StatisticsPvpModel: model.NewStatisticsPvpModel(gormDb),
|
|
RankPvpModel: model.NewRankPvpModel(gormDb),
|
|
}
|
|
}
|