sp.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package sp
  2. import (
  3. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/repositories"
  4. authService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/auth"
  5. cacheService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/cache"
  6. dbService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/db"
  7. envService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/env"
  8. mailService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/mailer"
  9. )
  10. type ServiceProvider struct {
  11. env *envService.Service
  12. cache *cacheService.Service
  13. mailer *mailService.Service
  14. auth *authService.Service
  15. articleRepository *repositories.ArticleRepository
  16. tagRepository *repositories.TagRepository
  17. articleTagRepository *repositories.ArticleTagRepository
  18. userRepository *repositories.UserRepository
  19. }
  20. func Init() (*ServiceProvider, error) {
  21. sp := &ServiceProvider{}
  22. // Init services
  23. env, err := envService.Init()
  24. if err != nil {
  25. return nil, err
  26. }
  27. sp.env = env
  28. cache, err := cacheService.Init()
  29. if err != nil {
  30. return nil, err
  31. }
  32. sp.cache = cache
  33. mailer, err := mailService.Init(sp.env)
  34. if err != nil {
  35. return nil, err
  36. }
  37. sp.mailer = mailer
  38. auth, err := authService.Init(sp.env)
  39. if err != nil {
  40. return nil, err
  41. }
  42. sp.auth = auth
  43. db, err := dbService.Init(env)
  44. if err != nil {
  45. return nil, err
  46. }
  47. // Init repositories
  48. sp.articleRepository = repositories.InitArticleRepository(db)
  49. sp.tagRepository = repositories.InitTagRepository(db)
  50. sp.articleTagRepository = repositories.InitArticleTagRepository(db)
  51. sp.userRepository = repositories.InitUserRepository(db)
  52. return sp, nil
  53. }
  54. func (sp *ServiceProvider) EnvService() *envService.Service {
  55. return sp.env
  56. }
  57. func (sp *ServiceProvider) CacheService() *cacheService.Service {
  58. return sp.cache
  59. }
  60. func (sp *ServiceProvider) MailerService() *mailService.Service {
  61. return sp.mailer
  62. }
  63. func (sp *ServiceProvider) AuthService() *authService.Service {
  64. return sp.auth
  65. }
  66. func (sp *ServiceProvider) ArticleRepository() *repositories.ArticleRepository {
  67. return sp.articleRepository
  68. }
  69. func (sp *ServiceProvider) TagRepository() *repositories.TagRepository {
  70. return sp.tagRepository
  71. }
  72. func (sp *ServiceProvider) ArticleTagRepository() *repositories.ArticleTagRepository {
  73. return sp.articleTagRepository
  74. }
  75. func (sp *ServiceProvider) UserRepository() *repositories.UserRepository {
  76. return sp.userRepository
  77. }