sp.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. ),
  54. )
  55. if err != nil {
  56. return nil, err
  57. }
  58. // Init transaction manager
  59. sp.tm = db.NewTransactionManager(dbService)
  60. // Init repositories
  61. sp.articleRepository = repositories.InitArticleRepository(dbService)
  62. sp.tagRepository = repositories.InitTagRepository(dbService)
  63. sp.articleTagRepository = repositories.InitArticleTagRepository(dbService)
  64. sp.userRepository = repositories.InitUserRepository(dbService)
  65. // Init logger
  66. loggerConfigOptions := logger.ConfigOptions{
  67. logger.WithEmailLogEnabled(configService.LoggerEmailEnabled()),
  68. logger.WithEmailLogLevel(configService.LoggerEmailLevel()),
  69. logger.WithEmailSubject(configService.LoggerEmailSubject()),
  70. logger.WithEmailRecipient(configService.LoggerEmailRecipient()),
  71. logger.WithEmailLogAddSource(configService.LoggerEmailAddSource()),
  72. logger.WithStdoutLogEnabled(configService.LoggerStdoutEnabled()),
  73. logger.WithStdoutLogLevel(configService.LoggerStdoutLevel()),
  74. logger.WithStdoutLogAddSource(configService.LoggerStdoutAddSource()),
  75. }
  76. if len(configService.SMTPPassword()) > 0 &&
  77. len(configService.SMTPUser()) > 0 &&
  78. len(configService.SMTPHost()) > 0 &&
  79. configService.SMTPPort() > 0 {
  80. smtpService, smtpErr := smtp.NewSMTP(
  81. smtp.NewConfig(
  82. smtp.WithPassword(configService.SMTPPassword()),
  83. smtp.WithUsername(configService.SMTPUser()),
  84. smtp.WithHost(configService.SMTPHost()),
  85. smtp.WithPort(configService.SMTPPort()),
  86. ),
  87. )
  88. if smtpErr != nil {
  89. return nil, smtpErr
  90. }
  91. loggerConfigOptions.Add(logger.WithSMTPClient(smtpService))
  92. }
  93. err = logger.Init(logger.NewConfig(loggerConfigOptions...))
  94. if err != nil {
  95. return nil, err
  96. }
  97. // Init translations
  98. if err = i18n.Init(); err != nil {
  99. return nil, err
  100. }
  101. return sp, nil
  102. }
  103. func (sp *ServiceProvider) ConfigService() *config.Service {
  104. return sp.config
  105. }
  106. func (sp *ServiceProvider) CacheService() *cache.Cache {
  107. return sp.cache
  108. }
  109. func (sp *ServiceProvider) AuthService() *auth.Service {
  110. return sp.auth
  111. }
  112. func (sp *ServiceProvider) TransactionManager() *db.TxManager {
  113. return sp.tm
  114. }
  115. func (sp *ServiceProvider) ArticleRepository() *repositories.ArticleRepository {
  116. return sp.articleRepository
  117. }
  118. func (sp *ServiceProvider) TagRepository() *repositories.TagRepository {
  119. return sp.tagRepository
  120. }
  121. func (sp *ServiceProvider) ArticleTagRepository() *repositories.ArticleTagRepository {
  122. return sp.articleTagRepository
  123. }
  124. func (sp *ServiceProvider) UserRepository() *repositories.UserRepository {
  125. return sp.userRepository
  126. }