sp.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 SMTP
  40. smtpService, err := smtp.NewSMTP(
  41. smtp.NewConfig(
  42. smtp.WithPassword(configService.SMTPPassword()),
  43. smtp.WithUsername(configService.SMTPUser()),
  44. smtp.WithHost(configService.SMTPHost()),
  45. smtp.WithPort(configService.SMTPPort()),
  46. ),
  47. )
  48. if err != nil {
  49. return nil, err
  50. }
  51. // Init DB
  52. dbService, err := db.NewDB(
  53. db.NewConfig(
  54. db.WithDriver(configService.DBDriver()),
  55. db.WithHost(configService.DBHost()),
  56. db.WithPort(configService.DBPort()),
  57. db.WithUsername(configService.DBUser()),
  58. db.WithPassword(configService.DBPassword()),
  59. db.WithDatabase(configService.DBName()),
  60. db.WithMaxIdleConns(configService.DBMaxIdleConns()),
  61. db.WithMaxOpenConns(configService.DBMaxOpenConns()),
  62. db.WithMaxIdleConnLifetime(configService.DBMaxIdleConnLifetime()),
  63. db.WithMaxOpenConnLifetime(configService.DBMaxOpenConnLifetime()),
  64. db.WithSSLMode(configService.DBSSLMode()),
  65. ),
  66. )
  67. if err != nil {
  68. return nil, err
  69. }
  70. // Init transaction manager
  71. sp.tm = db.NewTransactionManager(dbService)
  72. // Init repositories
  73. sp.placeRepository = repositories.InitPlaceRepository(dbService)
  74. sp.thingRepository = repositories.InitThingRepository(dbService)
  75. sp.placeThingRepository = repositories.InitPlaceThingRepository(dbService)
  76. sp.placeImageRepository = repositories.InitPlaceImageRepository(dbService)
  77. sp.thingImageRepository = repositories.InitThingImageRepository(dbService)
  78. sp.tagRepository = repositories.InitTagRepository(dbService)
  79. sp.thingTagRepository = repositories.InitThingTagRepository(dbService)
  80. sp.userRepository = repositories.InitUserRepository(dbService)
  81. sp.thingNotificationRepository = repositories.InitThingNotificationRepository(dbService)
  82. sp.fileRepository = repositories.InitFileRepository()
  83. // Init logger
  84. err = logger.Init(
  85. logger.NewConfig(
  86. logger.WithSMTPClient(smtpService),
  87. logger.WithEmailLogEnabled(configService.LoggerEmailEnabled()),
  88. logger.WithEmailLogLevel(configService.LoggerEmailLevel()),
  89. logger.WithEmailSubject(configService.LoggerEmailSubject()),
  90. logger.WithEmailRecipient(configService.LoggerEmailRecipient()),
  91. logger.WithEmailLogAddSource(configService.LoggerEmailAddSource()),
  92. logger.WithStdoutLogEnabled(configService.LoggerStdoutEnabled()),
  93. logger.WithStdoutLogLevel(configService.LoggerStdoutLevel()),
  94. logger.WithStdoutLogAddSource(configService.LoggerStdoutAddSource()),
  95. ),
  96. )
  97. if err != nil {
  98. return nil, err
  99. }
  100. return sp, nil
  101. }
  102. func (sp *ServiceProvider) ConfigService() *config.Service {
  103. return sp.config
  104. }
  105. func (sp *ServiceProvider) AuthService() *auth.Service {
  106. return sp.auth
  107. }
  108. func (sp *ServiceProvider) TransactionManager() *db.TxManager {
  109. return sp.tm
  110. }
  111. func (sp *ServiceProvider) PlaceRepository() *repositories.PlaceRepository {
  112. return sp.placeRepository
  113. }
  114. func (sp *ServiceProvider) ThingRepository() *repositories.ThingRepository {
  115. return sp.thingRepository
  116. }
  117. func (sp *ServiceProvider) TagRepository() *repositories.TagRepository {
  118. return sp.tagRepository
  119. }
  120. func (sp *ServiceProvider) PlaceThingRepository() *repositories.PlaceThingRepository {
  121. return sp.placeThingRepository
  122. }
  123. func (sp *ServiceProvider) PlaceImageRepository() *repositories.PlaceImageRepository {
  124. return sp.placeImageRepository
  125. }
  126. func (sp *ServiceProvider) ThingImageRepository() *repositories.ThingImageRepository {
  127. return sp.thingImageRepository
  128. }
  129. func (sp *ServiceProvider) ThingTagRepository() *repositories.ThingTagRepository {
  130. return sp.thingTagRepository
  131. }
  132. func (sp *ServiceProvider) ThingNotificationRepository() *repositories.ThingNotificationRepository {
  133. return sp.thingNotificationRepository
  134. }
  135. func (sp *ServiceProvider) UserRepository() *repositories.UserRepository {
  136. return sp.userRepository
  137. }
  138. func (sp *ServiceProvider) FileRepository() *repositories.FileRepository {
  139. return sp.fileRepository
  140. }