sp.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package sp
  2. import (
  3. "git.dmitriygnatenko.ru/dima/go-common/db"
  4. "git.dmitriygnatenko.ru/dima/go-common/logger"
  5. "git.dmitriygnatenko.ru/dima/go-common/smtp"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/repositories"
  7. "git.dmitriygnatenko.ru/dima/homethings/internal/services/auth"
  8. "git.dmitriygnatenko.ru/dima/homethings/internal/services/config"
  9. )
  10. type ServiceProvider struct {
  11. config *config.Service
  12. auth *auth.Service
  13. tm *db.TxManager
  14. placeRepository *repositories.PlaceRepository
  15. thingRepository *repositories.ThingRepository
  16. tagRepository *repositories.TagRepository
  17. placeThingRepository *repositories.PlaceThingRepository
  18. placeImageRepository *repositories.PlaceImageRepository
  19. thingImageRepository *repositories.ThingImageRepository
  20. thingTagRepository *repositories.ThingTagRepository
  21. userRepository *repositories.UserRepository
  22. thingNotificationRepository *repositories.ThingNotificationRepository
  23. fileRepository *repositories.FileRepository
  24. }
  25. func Init() (*ServiceProvider, error) {
  26. sp := &ServiceProvider{}
  27. // Init config
  28. configService, err := config.Init()
  29. if err != nil {
  30. return nil, err
  31. }
  32. sp.config = configService
  33. // Init auth
  34. authService, err := auth.Init(configService)
  35. if err != nil {
  36. return nil, err
  37. }
  38. sp.auth = authService
  39. // Init DB
  40. dbService, err := db.NewDB(
  41. db.NewConfig(
  42. db.WithDriver(configService.DBDriver()),
  43. db.WithHost(configService.DBHost()),
  44. db.WithPort(configService.DBPort()),
  45. db.WithUsername(configService.DBUser()),
  46. db.WithPassword(configService.DBPassword()),
  47. db.WithDatabase(configService.DBName()),
  48. db.WithMaxIdleConns(configService.DBMaxIdleConns()),
  49. db.WithMaxOpenConns(configService.DBMaxOpenConns()),
  50. db.WithMaxIdleConnLifetime(configService.DBMaxIdleConnLifetime()),
  51. db.WithMaxOpenConnLifetime(configService.DBMaxOpenConnLifetime()),
  52. db.WithSSLMode(configService.DBSSLMode()),
  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.placeRepository = repositories.InitPlaceRepository(dbService)
  62. sp.thingRepository = repositories.InitThingRepository(dbService)
  63. sp.placeThingRepository = repositories.InitPlaceThingRepository(dbService)
  64. sp.placeImageRepository = repositories.InitPlaceImageRepository(dbService)
  65. sp.thingImageRepository = repositories.InitThingImageRepository(dbService)
  66. sp.tagRepository = repositories.InitTagRepository(dbService)
  67. sp.thingTagRepository = repositories.InitThingTagRepository(dbService)
  68. sp.userRepository = repositories.InitUserRepository(dbService)
  69. sp.thingNotificationRepository = repositories.InitThingNotificationRepository(dbService)
  70. sp.fileRepository = repositories.InitFileRepository()
  71. // Init logger
  72. loggerConfigOptions := logger.ConfigOptions{
  73. logger.WithEmailLogEnabled(configService.LoggerEmailEnabled()),
  74. logger.WithEmailLogLevel(configService.LoggerEmailLevel()),
  75. logger.WithEmailSubject(configService.LoggerEmailSubject()),
  76. logger.WithEmailRecipient(configService.LoggerEmailRecipient()),
  77. logger.WithEmailLogAddSource(configService.LoggerEmailAddSource()),
  78. logger.WithStdoutLogEnabled(configService.LoggerStdoutEnabled()),
  79. logger.WithStdoutLogLevel(configService.LoggerStdoutLevel()),
  80. logger.WithStdoutLogAddSource(configService.LoggerStdoutAddSource()),
  81. }
  82. if len(configService.SMTPPassword()) > 0 &&
  83. len(configService.SMTPUser()) > 0 &&
  84. len(configService.SMTPHost()) > 0 &&
  85. configService.SMTPPort() > 0 {
  86. smtpService, smtpErr := smtp.NewSMTP(
  87. smtp.NewConfig(
  88. smtp.WithPassword(configService.SMTPPassword()),
  89. smtp.WithUsername(configService.SMTPUser()),
  90. smtp.WithHost(configService.SMTPHost()),
  91. smtp.WithPort(configService.SMTPPort()),
  92. ),
  93. )
  94. if smtpErr != nil {
  95. return nil, smtpErr
  96. }
  97. loggerConfigOptions.Add(logger.WithSMTPClient(smtpService))
  98. }
  99. err = logger.Init(logger.NewConfig(loggerConfigOptions...))
  100. if err != nil {
  101. return nil, err
  102. }
  103. return sp, nil
  104. }
  105. func (sp *ServiceProvider) ConfigService() *config.Service {
  106. return sp.config
  107. }
  108. func (sp *ServiceProvider) AuthService() *auth.Service {
  109. return sp.auth
  110. }
  111. func (sp *ServiceProvider) TransactionManager() *db.TxManager {
  112. return sp.tm
  113. }
  114. func (sp *ServiceProvider) PlaceRepository() *repositories.PlaceRepository {
  115. return sp.placeRepository
  116. }
  117. func (sp *ServiceProvider) ThingRepository() *repositories.ThingRepository {
  118. return sp.thingRepository
  119. }
  120. func (sp *ServiceProvider) TagRepository() *repositories.TagRepository {
  121. return sp.tagRepository
  122. }
  123. func (sp *ServiceProvider) PlaceThingRepository() *repositories.PlaceThingRepository {
  124. return sp.placeThingRepository
  125. }
  126. func (sp *ServiceProvider) PlaceImageRepository() *repositories.PlaceImageRepository {
  127. return sp.placeImageRepository
  128. }
  129. func (sp *ServiceProvider) ThingImageRepository() *repositories.ThingImageRepository {
  130. return sp.thingImageRepository
  131. }
  132. func (sp *ServiceProvider) ThingTagRepository() *repositories.ThingTagRepository {
  133. return sp.thingTagRepository
  134. }
  135. func (sp *ServiceProvider) ThingNotificationRepository() *repositories.ThingNotificationRepository {
  136. return sp.thingNotificationRepository
  137. }
  138. func (sp *ServiceProvider) UserRepository() *repositories.UserRepository {
  139. return sp.userRepository
  140. }
  141. func (sp *ServiceProvider) FileRepository() *repositories.FileRepository {
  142. return sp.fileRepository
  143. }