64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package svc
|
|
|
|
import (
|
|
"code.30cm.net/digimon/app-cloudep-product-service/internal/config"
|
|
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/usecase"
|
|
repo "code.30cm.net/digimon/app-cloudep-product-service/pkg/repository"
|
|
uc "code.30cm.net/digimon/app-cloudep-product-service/pkg/usecase"
|
|
mgo "code.30cm.net/digimon/library-go/mongo"
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
CategoryUseCase usecase.CategoryUseCase
|
|
}
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
return &ServiceContext{
|
|
CategoryUseCase: MustCategory(c),
|
|
Config: c,
|
|
}
|
|
}
|
|
|
|
func MustCategory(c config.Config) usecase.CategoryUseCase {
|
|
// 準備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,
|
|
}
|
|
if c.Mongo.User != "" {
|
|
conf.User = c.Mongo.User
|
|
conf.Password = c.Mongo.Password
|
|
}
|
|
|
|
// 快取選項
|
|
cacheOpts := []cache.Option{
|
|
cache.WithExpiry(c.CacheExpireTime),
|
|
cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry),
|
|
}
|
|
dbOpts := []mon.Option{
|
|
mgo.SetCustomDecimalType(),
|
|
mgo.InitMongoOptions(*conf),
|
|
}
|
|
|
|
categoryRepo := repo.MustCategoryRepository(repo.CategoryRepositoryParam{
|
|
Conf: conf,
|
|
CacheConf: c.Cache,
|
|
CacheOpts: cacheOpts,
|
|
DBOpts: dbOpts,
|
|
})
|
|
|
|
return uc.MustCategoryUseCase(uc.CategoryUseCaseParam{
|
|
CategoryRepo: categoryRepo})
|
|
}
|