40 lines
892 B
Go
40 lines
892 B
Go
package svc
|
|
|
|
import (
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/user_management/internal/gen/dao/query"
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_grpc/pkg/config"
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_grpc/pkg/id_gen"
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_grpc/pkg/my_gorm"
|
|
"github.com/sony/sonyflake"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
Query *query.Query
|
|
IDGen *sonyflake.Sonyflake
|
|
}
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
svc := &ServiceContext{
|
|
Config: c,
|
|
IDGen: id_gen.New(),
|
|
}
|
|
|
|
//初始化redis client
|
|
redisClient := redis.NewClient(&redis.Options{
|
|
Addr: c.Redis[0].Host,
|
|
})
|
|
//初始化数据库
|
|
db, err := my_gorm.NewDBWithCache(mysql.Open(c.Mysql), &gorm.Config{}, redisClient)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
svc.Query = query.Use(db)
|
|
return svc
|
|
}
|