sp.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.WithStdoutLogEnabled(configService.LoggerStdoutEnabled()),
  72. logger.WithStdoutLogLevel(configService.LoggerStdoutLevel()),
  73. }
  74. if len(configService.SMTPPassword()) > 0 &&
  75. len(configService.SMTPUser()) > 0 &&
  76. len(configService.SMTPHost()) > 0 &&
  77. configService.SMTPPort() > 0 {
  78. smtpService, smtpErr := smtp.NewSMTP(
  79. smtp.NewConfig(
  80. smtp.WithPassword(configService.SMTPPassword()),
  81. smtp.WithUsername(configService.SMTPUser()),
  82. smtp.WithHost(configService.SMTPHost()),
  83. smtp.WithPort(configService.SMTPPort()),
  84. ),
  85. )
  86. if smtpErr != nil {
  87. return nil, smtpErr
  88. }
  89. loggerConfigOptions.Add(logger.WithSMTPClient(smtpService))
  90. }
  91. err = logger.Init(logger.NewConfig(loggerConfigOptions...))
  92. if err != nil {
  93. return nil, err
  94. }
  95. // Init translations
  96. if err = i18n.Init(); err != nil {
  97. return nil, err
  98. }
  99. return sp, nil
  100. }
  101. func (sp *ServiceProvider) ConfigService() *config.Service {
  102. return sp.config
  103. }
  104. func (sp *ServiceProvider) CacheService() *cache.Cache {
  105. return sp.cache
  106. }
  107. func (sp *ServiceProvider) AuthService() *auth.Service {
  108. return sp.auth
  109. }
  110. func (sp *ServiceProvider) TransactionManager() *db.TxManager {
  111. return sp.tm
  112. }
  113. func (sp *ServiceProvider) ArticleRepository() *repositories.ArticleRepository {
  114. return sp.articleRepository
  115. }
  116. func (sp *ServiceProvider) TagRepository() *repositories.TagRepository {
  117. return sp.tagRepository
  118. }
  119. func (sp *ServiceProvider) ArticleTagRepository() *repositories.ArticleTagRepository {
  120. return sp.articleTagRepository
  121. }
  122. func (sp *ServiceProvider) UserRepository() *repositories.UserRepository {
  123. return sp.userRepository
  124. }