sp.go 4.4 KB

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