2024-08-22 14:26:02 +00:00
|
|
|
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"
|
|
|
|
vi "code.30cm.net/digimon/library-go/validator"
|
2024-08-24 06:48:52 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
2024-08-22 14:26:02 +00:00
|
|
|
"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
|
|
|
|
|
2024-08-24 06:48:52 +00:00
|
|
|
// TODO 可優化項目,將連線數量以及 timeout 都便可設定
|
2024-08-22 14:26:02 +00:00
|
|
|
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{
|
2024-08-24 06:48:52 +00:00
|
|
|
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),
|
|
|
|
),
|
2024-08-22 14:26:02 +00:00
|
|
|
GenUIDUseCase: usecase.MustGenerateUseCase(usecase.GenerateUseCaseParam{
|
|
|
|
GenerateUIDRepo: mongo,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|