sp.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service_provider
  2. import (
  3. "github.com/dmitriygnatenko/internal/interfaces"
  4. "github.com/dmitriygnatenko/internal/repositories"
  5. cacheService "github.com/dmitriygnatenko/internal/services/cache"
  6. dbService "github.com/dmitriygnatenko/internal/services/db"
  7. envService "github.com/dmitriygnatenko/internal/services/env"
  8. mailService "github.com/dmitriygnatenko/internal/services/mailer"
  9. )
  10. type ServiceProvider struct {
  11. env interfaces.IEnv
  12. cache interfaces.ICache
  13. mailer interfaces.IMailer
  14. articleRepository interfaces.IArticleRepository
  15. tagRepository interfaces.ITagRepository
  16. articleTagRepository interfaces.IArticleTagRepository
  17. }
  18. func Init() (*ServiceProvider, error) {
  19. sp := &ServiceProvider{}
  20. // Init env service
  21. env, err := envService.Init()
  22. if err != nil {
  23. return nil, err
  24. }
  25. sp.env = env
  26. db, err := dbService.Init(env)
  27. if err != nil {
  28. return nil, err
  29. }
  30. // Init repositories
  31. sp.articleRepository = repositories.InitArticleRepository(db)
  32. sp.tagRepository = repositories.InitTagRepository(db)
  33. sp.articleTagRepository = repositories.InitArticleTagRepository(db)
  34. // Init cache service
  35. sp.cache = cacheService.Init()
  36. // Init mailer
  37. sp.mailer = mailService.Init(sp.env)
  38. return sp, nil
  39. }
  40. func (sp *ServiceProvider) GetEnvService() interfaces.IEnv {
  41. return sp.env
  42. }
  43. func (sp *ServiceProvider) GetCacheService() interfaces.ICache {
  44. return sp.cache
  45. }
  46. func (sp *ServiceProvider) GetMailerService() interfaces.IMailer {
  47. return sp.mailer
  48. }
  49. func (sp *ServiceProvider) GetArticleRepository() interfaces.IArticleRepository {
  50. return sp.articleRepository
  51. }
  52. func (sp *ServiceProvider) GetTagRepository() interfaces.ITagRepository {
  53. return sp.tagRepository
  54. }
  55. func (sp *ServiceProvider) GetArticleTagRepository() interfaces.IArticleTagRepository {
  56. return sp.articleTagRepository
  57. }