app-cloudep-member-server/internal/svc/service_context.go

57 lines
1.6 KiB
Go

package svc
import (
"app-cloudep-member-server/internal/config"
"app-cloudep-member-server/internal/domain"
domainUC "app-cloudep-member-server/internal/domain/usecase"
"app-cloudep-member-server/internal/model"
mgo "app-cloudep-member-server/internal/model/mongo"
"app-cloudep-member-server/internal/usecase"
"fmt"
ers "code.30cm.net/digimon/library-go/errors"
vi "code.30cm.net/digimon/library-go/validator"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type ServiceContext struct {
Config config.Config
Validate vi.Validate
AccountModel model.AccountModel
UserModel model.UserTableModel
AccountToUidModel model.AccountToUidModel
GenUIDUseCase domainUC.UIDGenerateUseCase
Redis redis.Redis
}
func NewServiceContext(c config.Config) *ServiceContext {
// 設置
ers.Scope = domain.Scope
sqlConn := sqlx.NewMysql(c.DB.DsnString)
newRedis, err := redis.NewRedis(c.RedisCluster, redis.Cluster())
if err != nil {
panic(err)
}
mongo := mgo.NewAutoIdModel(
fmt.Sprintf(
"%s://%s:%s", c.Mongo.Schema,
c.Mongo.Host, c.Mongo.Port),
c.Mongo.Database,
c.Mongo.Collection)
return &ServiceContext{
Config: c,
Validate: vi.MustValidator(vi.WithAccount("account")),
Redis: *newRedis,
UserModel: model.NewUserTableModel(sqlConn, c.Cache),
AccountToUidModel: model.NewAccountToUidModel(sqlConn, c.Cache),
AccountModel: model.NewAccountModel(sqlConn, c.Cache),
GenUIDUseCase: usecase.MustGenerateUseCase(usecase.GenerateUseCaseParam{
GenerateUIDRepo: mongo,
}),
}
}