123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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.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, 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
- }
|