Files
houston-be/appcontext/app.go
Shashank Shekhar aa8161ca8b INFRA-2911 | Product service, repo and handler (#373)
* INFRA-2911 | Product entity, modal and repository

* INFRA-2911 | Product service and handler

* INFRA-2911 | Product service test

* INFRA-2911 | Optimisations

* INFRA-2911 | Optimisations
2024-02-19 18:57:10 +05:30

244 lines
6.2 KiB
Go

package appcontext
import (
"github.com/slack-go/slack/socketmode"
"github.com/spf13/viper"
"gorm.io/gorm"
"houston/model/incident"
"houston/model/log"
productModel "houston/model/product"
"houston/model/severity"
"houston/model/tag"
"houston/model/team"
"houston/model/user"
conferenceActions "houston/pkg/conference"
"houston/pkg/google/googleDrive"
"houston/pkg/postgres"
"houston/pkg/rest"
rcaRepository "houston/repository/rca/impl"
"houston/repository/rcaInput"
"houston/service/conference"
"houston/service/documentService"
"houston/service/google"
incidentService "houston/service/incident/impl"
"houston/service/products"
rcaService "houston/service/rca/impl"
"houston/service/slack"
tagService "houston/service/tag"
)
type applicationContext struct {
db *gorm.DB
}
// todo: all the repo objects to be cleaned uo from here. appContext will only have services
type houstonServices struct {
logRepo *log.Repository
teamRepo *team.Repository
severityRepo *severity.Repository
incidentRepo *incident.Repository
slackService *slack.SlackService
incidentService *incidentService.IncidentServiceV2
tagRepo *tag.Repository
rcaRepository *rcaRepository.Repository
rcaInputRepository *rcaInput.Repository
userRepository *user.Repository
rcaService *rcaService.RcaService
driveService google.IDriveService
calendarService *conference.CalendarService
tagService *tagService.TagService
productsService products.ProductService
}
var appContext *applicationContext
var services *houstonServices
func InitiateContext() {
appContext = &applicationContext{
db: initDB(),
}
}
func InitializeServices() {
logRepo := initLogRepo()
severityRepo := initSeverityRepo()
teamRepo := initTeamRepo(logRepo)
slaService := initSlackService()
services = &houstonServices{
logRepo: logRepo,
teamRepo: teamRepo,
severityRepo: severityRepo,
incidentRepo: initIncidentRepo(severityRepo, logRepo, teamRepo, slaService.SocketModeClientWrapper.GetClient()),
slackService: slaService,
incidentService: initIncidentService(),
tagRepo: initTagRepo(),
rcaRepository: initRCARepo(),
rcaInputRepository: initRCAInputRepo(),
userRepository: initUserRepo(),
rcaService: initRCAService(),
driveService: initDriveService(),
calendarService: initCalendarService(),
tagService: initTagService(),
productsService: initProductsService(),
}
}
func initDB() *gorm.DB {
return postgres.NewGormClient(
viper.GetString("postgres.dsn"),
viper.GetString("postgres.connection.max.idle.time"),
viper.GetString("postgres.connection.max.lifetime"),
viper.GetInt("postgres.connections.max.idle"),
viper.GetInt("postgres.connections.max.open"),
)
}
func GetDB() *gorm.DB {
return appContext.db
}
func initLogRepo() *log.Repository {
return log.NewLogRepository(GetDB())
}
func GetLogRepo() *log.Repository {
return services.logRepo
}
func initTeamRepo(logRepo *log.Repository) *team.Repository {
return team.NewTeamRepository(GetDB(), logRepo)
}
func GetTeamRepo() *team.Repository {
return services.teamRepo
}
func initSeverityRepo() *severity.Repository {
return severity.NewSeverityRepository(GetDB())
}
func GetSeverityRepo() *severity.Repository {
return services.severityRepo
}
func initIncidentRepo(
severityRepo *severity.Repository,
logRepo *log.Repository,
teamRepo *team.Repository,
socketModeClient *socketmode.Client,
) *incident.Repository {
return incident.NewIncidentRepository(GetDB(), severityRepo, logRepo, teamRepo, socketModeClient)
}
func GetIncidentRepo() *incident.Repository {
return services.incidentRepo
}
func initSlackService() *slack.SlackService {
return slack.NewSlackService()
}
func getRestClient() *rest.HttpRestClientImpl {
return rest.NewHttpRestClient()
}
func initDocumentService() *documentService.ActionsImpl {
return documentService.NewActionsImpl(getRestClient())
}
func initCalendarService() *conference.CalendarService {
calendarActions := conferenceActions.GetCalendarActions()
calendarService := conference.NewCalendarService(calendarActions)
return calendarService
}
func initTagService() *tagService.TagService {
return tagService.NewTagService(GetDB())
}
func GetTagService() *tagService.TagService {
return services.tagService
}
func initProductsService() products.ProductService {
return products.NewProductService(productModel.NewProductRepo(GetDB()))
}
func GetProductsService() products.ProductService {
return services.productsService
}
func GetCalendarService() *conference.CalendarService {
return services.calendarService
}
func GetDocumentService() *documentService.ActionsImpl {
return initDocumentService()
}
func initRCAService() *rcaService.RcaService {
rcaService := rcaService.NewRcaService(
initIncidentService(), initSlackService(), initDocumentService(),
initRCARepo(), initRCAInputRepo(), initUserRepo(), initDriveService(),
)
return rcaService
}
func GetSlackService() *slack.SlackService {
return services.slackService
}
func initIncidentService() *incidentService.IncidentServiceV2 {
return incidentService.NewIncidentServiceV2(GetDB())
}
func GetIncidentService() *incidentService.IncidentServiceV2 {
return services.incidentService
}
func GetRCAService() *rcaService.RcaService {
return services.rcaService
}
func initTagRepo() *tag.Repository {
return tag.NewTagRepository(GetDB())
}
func GetTagRepo() *tag.Repository {
return services.tagRepo
}
func initRCARepo() *rcaRepository.Repository {
return rcaRepository.NewRcaRepository(GetDB())
}
func GetRCARepo() *rcaRepository.Repository {
return services.rcaRepository
}
func initRCAInputRepo() *rcaInput.Repository {
return rcaInput.NewRcaInputRepository(GetDB())
}
func GetRCAInputRepo() *rcaInput.Repository {
return services.rcaInputRepository
}
func initUserRepo() *user.Repository {
return user.NewUserRepository(GetDB())
}
func GetUserRepo() *user.Repository {
return services.userRepository
}
func initDriveService() google.IDriveService {
driveAction, err := googleDrive.NewGoogleDriveActions()
if err != nil {
return nil
}
return google.NewDriveService(driveAction)
}
func GetDriveService() google.IDriveService {
return services.driveService
}