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" ers "code.30cm.net/digimon/library-go/errors" "code.30cm.net/digimon/library-go/errors/code" vi "code.30cm.net/digimon/library-go/validator" "fmt" "github.com/zeromicro/go-zero/core/stores/cache" "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 = code.CloudEPMember // TODO 可優化項目,將連線數量以及 timeout 都便可設定 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, cache.WithExpiry(domain.DefaultSingleFlyCacheTimeout), cache.WithNotFoundExpiry(domain.DefaultFindDataNotFoundTimeout)), AccountToUidModel: model.NewAccountToUidModel(sqlConn, c.Cache, cache.WithExpiry(domain.DefaultSingleFlyCacheTimeout), cache.WithNotFoundExpiry(domain.DefaultFindDataNotFoundTimeout)), AccountModel: model.NewAccountModel(sqlConn, c.Cache, cache.WithExpiry(domain.DefaultSingleFlyCacheTimeout), cache.WithNotFoundExpiry(domain.DefaultFindDataNotFoundTimeout), ), GenUIDUseCase: usecase.MustGenerateUseCase(usecase.GenerateUseCaseParam{ GenerateUIDRepo: mongo, }), } }