package sp import ( cache "git.dmitriygnatenko.ru/dima/go-common/cache/ttl_memory_cache" "git.dmitriygnatenko.ru/dima/go-common/db" "git.dmitriygnatenko.ru/dima/go-common/logger" "git.dmitriygnatenko.ru/dima/go-common/smtp" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/repositories" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/auth" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/config" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n" ) type ServiceProvider struct { config *config.Service auth *auth.Service cache *cache.Cache tm *db.TxManager articleRepository *repositories.ArticleRepository tagRepository *repositories.TagRepository articleTagRepository *repositories.ArticleTagRepository userRepository *repositories.UserRepository } func Init() (*ServiceProvider, error) { sp := &ServiceProvider{} // Init services configService, err := config.Init() if err != nil { return nil, err } sp.config = configService sp.cache = cache.NewCache( cache.NewConfig( cache.WithExpiration(configService.CacheDefaultDuration()), cache.WithCleanupInterval(configService.CacheCleanupInterval()), ), ) authService, err := auth.Init(sp.config) if err != nil { return nil, err } sp.auth = authService dbService, err := db.NewDB( db.NewConfig( db.WithDriver(configService.DBDriver()), db.WithHost(configService.DBHost()), db.WithPort(configService.DBPort()), db.WithUsername(configService.DBUser()), db.WithPassword(configService.DBPassword()), db.WithDatabase(configService.DBName()), db.WithMaxIdleConns(configService.DBMaxIdleConns()), db.WithMaxOpenConns(configService.DBMaxOpenConns()), db.WithMaxIdleConnLifetime(configService.DBMaxOpenConnLifetime()), db.WithMaxOpenConnLifetime(configService.DBMaxOpenConnLifetime()), ), ) if err != nil { return nil, err } // Init transaction manager sp.tm = db.NewTransactionManager(dbService) // Init repositories sp.articleRepository = repositories.InitArticleRepository(dbService) sp.tagRepository = repositories.InitTagRepository(dbService) sp.articleTagRepository = repositories.InitArticleTagRepository(dbService) sp.userRepository = repositories.InitUserRepository(dbService) // Init logger loggerConfigOptions := logger.ConfigOptions{ logger.WithEmailLogEnabled(configService.LoggerEmailEnabled()), logger.WithEmailLogLevel(configService.LoggerEmailLevel()), logger.WithEmailSubject(configService.LoggerEmailSubject()), logger.WithEmailRecipient(configService.LoggerEmailRecipient()), logger.WithEmailLogAddSource(configService.LoggerEmailAddSource()), logger.WithStdoutLogEnabled(configService.LoggerStdoutEnabled()), logger.WithStdoutLogLevel(configService.LoggerStdoutLevel()), logger.WithStdoutLogAddSource(configService.LoggerStdoutAddSource()), } if len(configService.SMTPPassword()) > 0 && len(configService.SMTPUser()) > 0 && len(configService.SMTPHost()) > 0 && configService.SMTPPort() > 0 { smtpService, smtpErr := smtp.NewSMTP( smtp.NewConfig( smtp.WithPassword(configService.SMTPPassword()), smtp.WithUsername(configService.SMTPUser()), smtp.WithHost(configService.SMTPHost()), smtp.WithPort(configService.SMTPPort()), ), ) if smtpErr != nil { return nil, smtpErr } loggerConfigOptions.Add(logger.WithSMTPClient(smtpService)) } err = logger.Init(logger.NewConfig(loggerConfigOptions...)) if err != nil { return nil, err } // Init translations if err = i18n.Init(); err != nil { return nil, err } return sp, nil } func (sp *ServiceProvider) ConfigService() *config.Service { return sp.config } func (sp *ServiceProvider) CacheService() *cache.Cache { return sp.cache } func (sp *ServiceProvider) AuthService() *auth.Service { return sp.auth } func (sp *ServiceProvider) TransactionManager() *db.TxManager { return sp.tm } func (sp *ServiceProvider) ArticleRepository() *repositories.ArticleRepository { return sp.articleRepository } func (sp *ServiceProvider) TagRepository() *repositories.TagRepository { return sp.tagRepository } func (sp *ServiceProvider) ArticleTagRepository() *repositories.ArticleTagRepository { return sp.articleTagRepository } func (sp *ServiceProvider) UserRepository() *repositories.UserRepository { return sp.userRepository }