qnc-server-tob/app/user/cmd/api/internal/svc/servicecontext.go
2025-01-10 00:09:25 +08:00

87 lines
3.4 KiB
Go

package svc
import (
"github.com/hibiken/asynq"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/rest"
"tydata-server/app/user/cmd/api/internal/config"
"tydata-server/app/user/cmd/api/internal/middleware"
"tydata-server/app/user/cmd/api/internal/service"
"tydata-server/app/user/model"
)
type ServiceContext struct {
Config config.Config
Redis *redis.Redis
SourceInterceptor rest.Middleware
UserModel model.UserModel
UserAuthModel model.UserAuthModel
ProductModel model.ProductModel
FeatureModel model.FeatureModel
ProductFeatureModel model.ProductFeatureModel
OrderModel model.OrderModel
QueryModel model.QueryModel
GlobalNotificationsModel model.GlobalNotificationsModel
AlipayService *service.AliPayService
WechatPayService *service.WechatPayService
ApplePayService *service.ApplePayService
WestDexService *service.WestDexService
YushanService *service.YushanService
ApiRequestService *service.ApiRequestService
AsynqServer *asynq.Server // 服务端
AsynqService *service.AsynqService // 客户端
VerificationService *service.VerificationService
}
func NewServiceContext(c config.Config) *ServiceContext {
db := sqlx.NewMysql(c.DataSource)
redisConf := redis.RedisConf{
Host: c.CacheRedis[0].Host,
Pass: c.CacheRedis[0].Pass,
Type: c.CacheRedis[0].Type,
}
asynqServer := asynq.NewServer(
asynq.RedisClientOpt{Addr: c.CacheRedis[0].Host, Password: c.CacheRedis[0].Pass},
asynq.Config{
IsFailure: func(err error) bool {
logx.Errorf("异步任务失败: %+v \n", err)
return true
},
Concurrency: 10,
},
)
westDexService := service.NewWestDexService(c)
yushanService := service.NewYushanService(c)
productFeatureModel := model.NewProductFeatureModel(db, c.CacheRedis)
featureModel := model.NewFeatureModel(db, c.CacheRedis)
return &ServiceContext{
Config: c,
Redis: redis.MustNewRedis(redisConf),
SourceInterceptor: middleware.NewSourceInterceptorMiddleware().Handle,
AlipayService: service.NewAliPayService(c),
WechatPayService: service.NewWechatPayService(c),
ApplePayService: service.NewApplePayService(c),
WestDexService: westDexService,
YushanService: yushanService,
VerificationService: service.NewVerificationService(c, westDexService),
AsynqServer: asynqServer,
ApiRequestService: service.NewApiRequestService(c, westDexService, yushanService, featureModel, productFeatureModel),
AsynqService: service.NewAsynqService(c),
UserModel: model.NewUserModel(db, c.CacheRedis),
UserAuthModel: model.NewUserAuthModel(db, c.CacheRedis),
ProductModel: model.NewProductModel(db, c.CacheRedis),
OrderModel: model.NewOrderModel(db, c.CacheRedis),
QueryModel: model.NewQueryModel(db, c.CacheRedis),
GlobalNotificationsModel: model.NewGlobalNotificationsModel(db, c.CacheRedis),
FeatureModel: featureModel,
ProductFeatureModel: productFeatureModel,
}
}
func (s *ServiceContext) Close() {
if s.AsynqService != nil {
s.AsynqService.Close()
}
}