sp.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package sp
  2. import (
  3. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/interfaces"
  4. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/repositories"
  5. authService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/auth"
  6. cacheService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/cache"
  7. dbService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/db"
  8. envService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/env"
  9. mailService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/mailer"
  10. )
  11. type ServiceProvider struct {
  12. env interfaces.Env
  13. cache interfaces.Cache
  14. mailer interfaces.Mailer
  15. auth interfaces.Auth
  16. articleRepository interfaces.ArticleRepository
  17. tagRepository interfaces.TagRepository
  18. articleTagRepository interfaces.ArticleTagRepository
  19. userRepository interfaces.UserRepository
  20. }
  21. func Init() (interfaces.ServiceProvider, error) {
  22. sp := &ServiceProvider{}
  23. // Init services
  24. env, err := envService.Init()
  25. if err != nil {
  26. return nil, err
  27. }
  28. sp.env = env
  29. cache, err := cacheService.Init()
  30. if err != nil {
  31. return nil, err
  32. }
  33. sp.cache = cache
  34. mailer, err := mailService.Init(sp.env)
  35. if err != nil {
  36. return nil, err
  37. }
  38. sp.mailer = mailer
  39. auth, err := authService.Init(sp.env)
  40. if err != nil {
  41. return nil, err
  42. }
  43. sp.auth = auth
  44. db, err := dbService.Init(env)
  45. if err != nil {
  46. return nil, err
  47. }
  48. // Init repositories
  49. sp.articleRepository = repositories.InitArticleRepository(db)
  50. sp.tagRepository = repositories.InitTagRepository(db)
  51. sp.articleTagRepository = repositories.InitArticleTagRepository(db)
  52. sp.userRepository = repositories.InitUserRepository(db)
  53. return sp, nil
  54. }
  55. func (sp *ServiceProvider) GetEnvService() interfaces.Env {
  56. return sp.env
  57. }
  58. func (sp *ServiceProvider) GetCacheService() interfaces.Cache {
  59. return sp.cache
  60. }
  61. func (sp *ServiceProvider) GetMailerService() interfaces.Mailer {
  62. return sp.mailer
  63. }
  64. func (sp *ServiceProvider) GetAuthService() interfaces.Auth {
  65. return sp.auth
  66. }
  67. func (sp *ServiceProvider) GetArticleRepository() interfaces.ArticleRepository {
  68. return sp.articleRepository
  69. }
  70. func (sp *ServiceProvider) GetTagRepository() interfaces.TagRepository {
  71. return sp.tagRepository
  72. }
  73. func (sp *ServiceProvider) GetArticleTagRepository() interfaces.ArticleTagRepository {
  74. return sp.articleTagRepository
  75. }
  76. func (sp *ServiceProvider) GetUserRepository() interfaces.UserRepository {
  77. return sp.userRepository
  78. }
  79. func InitMock(deps ...interface{}) interfaces.ServiceProvider {
  80. sp := ServiceProvider{}
  81. for _, d := range deps {
  82. switch s := d.(type) {
  83. case interfaces.Cache:
  84. sp.cache = s
  85. case interfaces.Auth:
  86. sp.auth = s
  87. case interfaces.Env:
  88. sp.env = s
  89. case interfaces.Mailer:
  90. sp.mailer = s
  91. case interfaces.ArticleRepository:
  92. sp.articleRepository = s
  93. case interfaces.ArticleTagRepository:
  94. sp.articleTagRepository = s
  95. case interfaces.TagRepository:
  96. sp.tagRepository = s
  97. case interfaces.UserRepository:
  98. sp.userRepository = s
  99. }
  100. }
  101. return &sp
  102. }