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

132 lines
3.7 KiB
Go
Raw Normal View History

2024-12-30 03:58:14 +00:00
package svc
import (
2025-02-08 01:59:21 +00:00
"context"
2025-02-04 08:58:01 +00:00
"code.30cm.net/digimon/app-cloudep-member-server/internal/config"
cfg "code.30cm.net/digimon/app-cloudep-member-server/pkg/domain/config"
"code.30cm.net/digimon/app-cloudep-member-server/pkg/domain/usecase"
"code.30cm.net/digimon/app-cloudep-member-server/pkg/repository"
uc "code.30cm.net/digimon/app-cloudep-member-server/pkg/usecase"
2024-12-30 03:58:14 +00:00
"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,
}
2025-02-08 01:59:21 +00:00
if c.Mongo.User != "" {
conf.User = c.Mongo.User
conf.Password = c.Mongo.Password
}
2024-12-30 03:58:14 +00:00
// 快取選項
cacheOpts := []cache.Option{
cache.WithExpiry(c.CacheExpireTime),
cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry),
}
dbOpts := []mon.Option{
mgo.SetCustomDecimalType(),
mgo.InitMongoOptions(*conf),
}
2025-02-08 01:59:21 +00:00
newRedis, err := redis.NewRedis(c.Redis.RedisConf)
2024-12-30 03:58:14 +00:00
if err != nil {
panic(err)
}
2025-02-08 01:59:21 +00:00
ac := repository.NewAccountRepository(repository.AccountRepositoryParam{
Conf: conf,
CacheConf: c.Cache,
CacheOpts: cacheOpts,
DBOpts: dbOpts,
})
u := repository.NewUserRepository(repository.UserRepositoryParam{
Conf: conf,
CacheConf: c.Cache,
CacheOpts: cacheOpts,
DBOpts: dbOpts,
})
guid := repository.NewAutoIDRepository(repository.AutoIDRepositoryParam{
Conf: conf,
DBOpts: dbOpts,
})
auid := repository.NewAccountUIDRepository(repository.AccountUIDRepositoryParam{
Conf: conf,
CacheConf: c.Cache,
CacheOpts: cacheOpts,
DBOpts: dbOpts,
})
_, _ = ac.Index20241226001UP(context.Background())
_, _ = u.Index20241226001UP(context.Background())
_, _ = guid.Index20241226001UP(context.Background())
_, _ = auid.Index20241226001UP(context.Background())
2024-12-30 03:58:14 +00:00
return uc.MustMemberUseCase(uc.MemberUseCaseParam{
2025-02-08 01:59:21 +00:00
Account: ac,
User: u,
AccountUID: auid,
2024-12-30 03:58:14 +00:00
VerifyCodeModel: repository.NewVerifyCodeRepository(newRedis),
2025-02-08 01:59:21 +00:00
GenerateUID: guid,
Config: prepareCfg(c),
2024-12-30 03:58:14 +00:00
})
}
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,
},
}
}