package sp import ( "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/interfaces" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/repositories" authService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/auth" cacheService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/cache" dbService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/db" envService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/env" mailService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/mailer" ) type ServiceProvider struct { env interfaces.Env cache interfaces.Cache mailer interfaces.Mailer auth interfaces.Auth articleRepository interfaces.ArticleRepository tagRepository interfaces.TagRepository articleTagRepository interfaces.ArticleTagRepository userRepository interfaces.UserRepository } func Init() (interfaces.ServiceProvider, error) { sp := &ServiceProvider{} // Init services env, err := envService.Init() if err != nil { return nil, err } sp.env = env cache, err := cacheService.Init() if err != nil { return nil, err } sp.cache = cache mailer, err := mailService.Init(sp.env) if err != nil { return nil, err } sp.mailer = mailer auth, err := authService.Init(sp.env) if err != nil { return nil, err } sp.auth = auth db, err := dbService.Init(env) if err != nil { return nil, err } // Init repositories sp.articleRepository = repositories.InitArticleRepository(db) sp.tagRepository = repositories.InitTagRepository(db) sp.articleTagRepository = repositories.InitArticleTagRepository(db) sp.userRepository = repositories.InitUserRepository(db) return sp, nil } func (sp *ServiceProvider) GetEnvService() interfaces.Env { return sp.env } func (sp *ServiceProvider) GetCacheService() interfaces.Cache { return sp.cache } func (sp *ServiceProvider) GetMailerService() interfaces.Mailer { return sp.mailer } func (sp *ServiceProvider) GetAuthService() interfaces.Auth { return sp.auth } func (sp *ServiceProvider) GetArticleRepository() interfaces.ArticleRepository { return sp.articleRepository } func (sp *ServiceProvider) GetTagRepository() interfaces.TagRepository { return sp.tagRepository } func (sp *ServiceProvider) GetArticleTagRepository() interfaces.ArticleTagRepository { return sp.articleTagRepository } func (sp *ServiceProvider) GetUserRepository() interfaces.UserRepository { return sp.userRepository } func InitMock(deps ...interface{}) interfaces.ServiceProvider { sp := ServiceProvider{} for _, d := range deps { switch s := d.(type) { case interfaces.Cache: sp.cache = s case interfaces.Auth: sp.auth = s case interfaces.Env: sp.env = s case interfaces.Mailer: sp.mailer = s case interfaces.ArticleRepository: sp.articleRepository = s case interfaces.ArticleTagRepository: sp.articleTagRepository = s case interfaces.TagRepository: sp.tagRepository = s case interfaces.UserRepository: sp.userRepository = s } } return &sp }