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

66 lines
2.1 KiB
Go
Raw Normal View History

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"
2024-08-26 08:33:28 +00:00
"code.30cm.net/digimon/library-go/errors/code"
vi "code.30cm.net/digimon/library-go/validator"
2024-08-24 06:46:26 +00:00
"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 {
// 設置
2024-08-26 08:33:28 +00:00
ers.Scope = code.CloudEPMember
2024-08-24 06:46:26 +00:00
// 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{
2024-08-24 06:46:26 +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),
),
GenUIDUseCase: usecase.MustGenerateUseCase(usecase.GenerateUseCaseParam{
GenerateUIDRepo: mongo,
}),
}
}