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

115 lines
3.2 KiB
Go
Raw Permalink Normal View History

2024-12-30 03:58:14 +00:00
package svc
import (
"app-cloudep-member-server/internal/config"
cfg "app-cloudep-member-server/pkg/domain/config"
"app-cloudep-member-server/pkg/domain/usecase"
"app-cloudep-member-server/pkg/repository"
uc "app-cloudep-member-server/pkg/usecase"
"code.30cm.net/digimon/library-go/errs"
"code.30cm.net/digimon/library-go/errs/code"
mgo "code.30cm.net/digimon/library-go/mongo"
vi "code.30cm.net/digimon/library-go/validator"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/mon"
"github.com/zeromicro/go-zero/core/stores/redis"
)
type ServiceContext struct {
Config config.Config
Validate vi.Validate
AccountUseCase usecase.AccountUseCase
}
func NewServiceContext(c config.Config) *ServiceContext {
// 設置
errs.Scope = code.CloudEPMember
return &ServiceContext{
Config: c,
Validate: vi.MustValidator(vi.WithAccount("account")),
AccountUseCase: NewAccountUC(&c),
}
}
func NewAccountUC(c *config.Config) usecase.AccountUseCase {
// 準備Mongo Config
conf := &mgo.Conf{
Schema: c.Mongo.Schema,
Host: c.Mongo.Host,
Database: c.Mongo.Database,
MaxStaleness: c.Mongo.MaxStaleness,
MaxPoolSize: c.Mongo.MaxPoolSize,
MinPoolSize: c.Mongo.MinPoolSize,
MaxConnIdleTime: c.Mongo.MaxConnIdleTime,
Compressors: c.Mongo.Compressors,
EnableStandardReadWriteSplitMode: c.Mongo.EnableStandardReadWriteSplitMode,
ConnectTimeoutMs: c.Mongo.ConnectTimeoutMs,
}
// 快取選項
cacheOpts := []cache.Option{
cache.WithExpiry(c.CacheExpireTime),
cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry),
}
dbOpts := []mon.Option{
mgo.SetCustomDecimalType(),
mgo.InitMongoOptions(*conf),
}
newRedis, err := redis.NewRedis(c.RedisConf, redis.Cluster())
if err != nil {
panic(err)
}
return uc.MustMemberUseCase(uc.MemberUseCaseParam{
Account: repository.NewAccountRepository(repository.AccountRepositoryParam{
Conf: conf,
CacheConf: c.Cache,
CacheOpts: cacheOpts,
DbOpts: dbOpts,
}),
User: repository.NewUserRepository(repository.UserRepositoryParam{
Conf: conf,
CacheConf: c.Cache,
CacheOpts: cacheOpts,
DbOpts: dbOpts,
}),
AccountUID: repository.NewAccountUIDRepository(repository.AccountUIDRepositoryParam{
Conf: conf,
CacheConf: c.Cache,
CacheOpts: cacheOpts,
DbOpts: dbOpts,
}),
VerifyCodeModel: repository.NewVerifyCodeRepository(newRedis),
GenerateUID: repository.NewAutoIDRepository(repository.AutoIDRepositoryParam{
Conf: conf,
DbOpts: dbOpts,
}),
Config: prepareCfg(c),
})
}
func prepareCfg(c *config.Config) cfg.Config {
return cfg.Config{
Bcrypt: struct{ Cost int }{Cost: c.Bcrypt.Cost},
GoogleAuth: struct {
ClientID string
AuthURL string
}{
ClientID: c.GoogleAuth.ClientID,
AuthURL: c.GoogleAuth.AuthURL,
},
LineAuth: struct {
ClientID string
ClientSecret string
RedirectURI string
}{
ClientID: c.LineAuth.ClientID,
ClientSecret: c.LineAuth.ClientSecret,
RedirectURI: c.LineAuth.RedirectURI,
},
}
}