123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package sp
- import (
- "context"
- 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/helpers/errors"
- "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
- languageRepository *repositories.LanguageRepository
- }
- func Init() (*ServiceProvider, error) {
- sp := &ServiceProvider{}
- // Init services
- configService, err := config.Init()
- if err != nil {
- return nil, errors.Wrap(err, "can't init config")
- }
- 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, errors.Wrap(err, "can't init auth service")
- }
- 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, errors.Wrap(err, "can't init DB service")
- }
- // 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)
- sp.languageRepository = repositories.InitLanguageRepository(dbService)
- // Init logger
- loggerConfigOptions := logger.ConfigOptions{
- logger.WithEmailLogEnabled(configService.LoggerEmailEnabled()),
- logger.WithEmailLogLevel(configService.LoggerEmailLevel()),
- logger.WithEmailSubject(configService.LoggerEmailSubject()),
- logger.WithEmailRecipient(configService.LoggerEmailRecipient()),
- logger.WithStdoutLogEnabled(configService.LoggerStdoutEnabled()),
- logger.WithStdoutLogLevel(configService.LoggerStdoutLevel()),
- }
- 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, errors.Wrap(smtpErr, "can't init SMTP service")
- }
- loggerConfigOptions.Add(logger.WithSMTPClient(smtpService))
- }
- err = logger.Init(logger.NewConfig(loggerConfigOptions...))
- if err != nil {
- return nil, errors.Wrap(err, "can't init logger service")
- }
- languages, err := sp.languageRepository.GetAll(context.Background())
- if err != nil {
- return nil, errors.Wrap(err, "can't get languages")
- }
- // Init translations
- if err = i18n.Init(languages); err != nil {
- return nil, errors.Wrap(err, "can't init I18N service")
- }
- 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
- }
- func (sp *ServiceProvider) LanguageRepository() *repositories.LanguageRepository {
- return sp.languageRepository
- }
|