sp.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package sp
  2. import (
  3. "context"
  4. cache "git.dmitriygnatenko.ru/dima/go-common/cache/ttl_memory_cache"
  5. "git.dmitriygnatenko.ru/dima/go-common/db"
  6. "git.dmitriygnatenko.ru/dima/go-common/logger"
  7. "git.dmitriygnatenko.ru/dima/go-common/smtp"
  8. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helpers/errors"
  9. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/repositories"
  10. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/auth"
  11. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/config"
  12. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
  13. )
  14. type ServiceProvider struct {
  15. config *config.Service
  16. auth *auth.Service
  17. cache *cache.Cache
  18. tm *db.TxManager
  19. articleRepository *repositories.ArticleRepository
  20. tagRepository *repositories.TagRepository
  21. articleTagRepository *repositories.ArticleTagRepository
  22. userRepository *repositories.UserRepository
  23. languageRepository *repositories.LanguageRepository
  24. }
  25. func Init() (*ServiceProvider, error) {
  26. sp := &ServiceProvider{}
  27. // Init services
  28. configService, err := config.Init()
  29. if err != nil {
  30. return nil, errors.Wrap(err, "can't init config")
  31. }
  32. sp.config = configService
  33. sp.cache = cache.NewCache(
  34. cache.NewConfig(
  35. cache.WithExpiration(configService.CacheDefaultDuration()),
  36. cache.WithCleanupInterval(configService.CacheCleanupInterval()),
  37. ),
  38. )
  39. authService, err := auth.Init(sp.config)
  40. if err != nil {
  41. return nil, errors.Wrap(err, "can't init auth service")
  42. }
  43. sp.auth = authService
  44. dbService, err := db.NewDB(
  45. db.NewConfig(
  46. db.WithDriver(configService.DBDriver()),
  47. db.WithHost(configService.DBHost()),
  48. db.WithPort(configService.DBPort()),
  49. db.WithUsername(configService.DBUser()),
  50. db.WithPassword(configService.DBPassword()),
  51. db.WithDatabase(configService.DBName()),
  52. db.WithMaxIdleConns(configService.DBMaxIdleConns()),
  53. db.WithMaxOpenConns(configService.DBMaxOpenConns()),
  54. db.WithMaxIdleConnLifetime(configService.DBMaxOpenConnLifetime()),
  55. db.WithMaxOpenConnLifetime(configService.DBMaxOpenConnLifetime()),
  56. ),
  57. )
  58. if err != nil {
  59. return nil, errors.Wrap(err, "can't init DB service")
  60. }
  61. // Init transaction manager
  62. sp.tm = db.NewTransactionManager(dbService)
  63. // Init repositories
  64. sp.articleRepository = repositories.InitArticleRepository(dbService)
  65. sp.tagRepository = repositories.InitTagRepository(dbService)
  66. sp.articleTagRepository = repositories.InitArticleTagRepository(dbService)
  67. sp.userRepository = repositories.InitUserRepository(dbService)
  68. sp.languageRepository = repositories.InitLanguageRepository(dbService)
  69. // Init logger
  70. loggerConfigOptions := logger.ConfigOptions{
  71. logger.WithEmailLogEnabled(configService.LoggerEmailEnabled()),
  72. logger.WithEmailLogLevel(configService.LoggerEmailLevel()),
  73. logger.WithEmailSubject(configService.LoggerEmailSubject()),
  74. logger.WithEmailRecipient(configService.LoggerEmailRecipient()),
  75. logger.WithStdoutLogEnabled(configService.LoggerStdoutEnabled()),
  76. logger.WithStdoutLogLevel(configService.LoggerStdoutLevel()),
  77. }
  78. if len(configService.SMTPPassword()) > 0 &&
  79. len(configService.SMTPUser()) > 0 &&
  80. len(configService.SMTPHost()) > 0 &&
  81. configService.SMTPPort() > 0 {
  82. smtpService, smtpErr := smtp.NewSMTP(
  83. smtp.NewConfig(
  84. smtp.WithPassword(configService.SMTPPassword()),
  85. smtp.WithUsername(configService.SMTPUser()),
  86. smtp.WithHost(configService.SMTPHost()),
  87. smtp.WithPort(configService.SMTPPort()),
  88. ),
  89. )
  90. if smtpErr != nil {
  91. return nil, errors.Wrap(smtpErr, "can't init SMTP service")
  92. }
  93. loggerConfigOptions.Add(logger.WithSMTPClient(smtpService))
  94. }
  95. err = logger.Init(logger.NewConfig(loggerConfigOptions...))
  96. if err != nil {
  97. return nil, errors.Wrap(err, "can't init logger service")
  98. }
  99. languages, err := sp.languageRepository.GetAll(context.Background())
  100. if err != nil {
  101. return nil, errors.Wrap(err, "can't get languages")
  102. }
  103. // Init translations
  104. if err = i18n.Init(languages); err != nil {
  105. return nil, errors.Wrap(err, "can't init I18N service")
  106. }
  107. return sp, nil
  108. }
  109. func (sp *ServiceProvider) ConfigService() *config.Service {
  110. return sp.config
  111. }
  112. func (sp *ServiceProvider) CacheService() *cache.Cache {
  113. return sp.cache
  114. }
  115. func (sp *ServiceProvider) AuthService() *auth.Service {
  116. return sp.auth
  117. }
  118. func (sp *ServiceProvider) TransactionManager() *db.TxManager {
  119. return sp.tm
  120. }
  121. func (sp *ServiceProvider) ArticleRepository() *repositories.ArticleRepository {
  122. return sp.articleRepository
  123. }
  124. func (sp *ServiceProvider) TagRepository() *repositories.TagRepository {
  125. return sp.tagRepository
  126. }
  127. func (sp *ServiceProvider) ArticleTagRepository() *repositories.ArticleTagRepository {
  128. return sp.articleTagRepository
  129. }
  130. func (sp *ServiceProvider) UserRepository() *repositories.UserRepository {
  131. return sp.userRepository
  132. }
  133. func (sp *ServiceProvider) LanguageRepository() *repositories.LanguageRepository {
  134. return sp.languageRepository
  135. }