sp.go 5.0 KB

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