From 13b4e60a12a333a022404c70f2faf505368d1d97 Mon Sep 17 00:00:00 2001 From: Garrett Ladley <92384606+garrettladley@users.noreply.github.com> Date: Tue, 28 May 2024 17:16:44 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20chore:=20clean=20up=20config=20p?= =?UTF-8?q?arams=20to=20only=20pass=20necessary=20settings=20(#935)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/config/settings.go | 22 ++++++++++++++-------- backend/database/db.go | 8 ++++---- backend/main.go | 30 ++++++++++++------------------ backend/tests/api/helpers/app.go | 2 +- backend/tests/api/helpers/db.go | 10 +++++----- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/backend/config/settings.go b/backend/config/settings.go index c4856b8ac..39f36753b 100644 --- a/backend/config/settings.go +++ b/backend/config/settings.go @@ -8,11 +8,15 @@ type Settings struct { RedisLimiter RedisSettings SuperUser SuperUserSettings Auth AuthSettings - AWS AWSSettings - Resend ResendSettings Calendar CalendarSettings - Google OAuthSettings - Outlook OAuthSettings + Integrations +} + +type Integrations struct { + GoogleOauth OAuthSettings + OutlookOauth OAuthSettings + AWS AWSSettings + Resend ResendSettings } type intermediateSettings struct { @@ -94,10 +98,12 @@ func (i *intermediateSettings) into() (*Settings, error) { RedisLimiter: *redisLimiter, SuperUser: *superUser, Auth: *auth, - AWS: *aws, - Resend: *resend, Calendar: *calendar, - Google: *google, - Outlook: *outlook, + Integrations: Integrations{ + GoogleOauth: *google, + OutlookOauth: *outlook, + AWS: *aws, + Resend: *resend, + }, }, nil } diff --git a/backend/database/db.go b/backend/database/db.go index c2652aefa..928dd1b0b 100644 --- a/backend/database/db.go +++ b/backend/database/db.go @@ -17,7 +17,7 @@ func ConfigureDB(settings config.Settings) (*gorm.DB, error) { return nil, err } - if err := CreateSuperUserIfNotExists(settings, db); err != nil { + if err := CreateSuperUserIfNotExists(settings.SuperUser, db); err != nil { return nil, err } @@ -64,7 +64,7 @@ func ConnPooling(db *gorm.DB) error { return nil } -func CreateSuperUserIfNotExists(settings config.Settings, db *gorm.DB) error { +func CreateSuperUserIfNotExists(settings config.SuperUserSettings, db *gorm.DB) error { var superUser models.User if err := db.Where("role = ?", models.Super).First(&superUser).Error; err != nil { @@ -80,7 +80,7 @@ func CreateSuperUserIfNotExists(settings config.Settings, db *gorm.DB) error { return nil } -func createSuperUser(settings config.Settings, db *gorm.DB) error { +func createSuperUser(settings config.SuperUserSettings, db *gorm.DB) error { tx := db.Begin() defer func() { if r := recover(); r != nil { @@ -92,7 +92,7 @@ func createSuperUser(settings config.Settings, db *gorm.DB) error { return err } - superUser, err := SuperUser(settings.SuperUser) + superUser, err := SuperUser(settings) if err != nil { tx.Rollback() return err diff --git a/backend/main.go b/backend/main.go index 23cddfc9d..aab1fa2bc 100644 --- a/backend/main.go +++ b/backend/main.go @@ -64,7 +64,7 @@ func main() { stores := store.ConfigureRedis(*config) - integrations := configureIntegrations(config) + integrations := configureIntegrations(&config.Integrations) tp := telemetry.InitTracer() @@ -76,7 +76,7 @@ func main() { } }() - app := server.Init(db, stores, integrations, *config) + app := server.Init(db, stores, *integrations, *config) err = app.Listen(fmt.Sprintf("%s:%d", config.Application.Host, config.Application.Port)) if err != nil { @@ -94,23 +94,17 @@ func checkServerRunning(host string, port uint16) error { return nil } -func configureIntegrations(config *config.Settings) integrations.Integrations { - googleClient := oauth.GoogleOAuthClient{ - OAuthConfig: config.Google, - } - outlookClient := oauth.OutlookOAuthClient{ - OAuthConfig: config.Outlook, - } - - oauthResources := oauth.OauthProviderSettings{ - Google: googleClient, - Outlook: outlookClient, - } - - integrations := integrations.Integrations{ +func configureIntegrations(config *config.Integrations) *integrations.Integrations { + return &integrations.Integrations{ File: file.NewAWSProvider(config.AWS), Email: email.NewResendClient(config.Resend, true), - OAuth: oauthResources, + OAuth: oauth.OauthProviderSettings{ + Google: oauth.GoogleOAuthClient{ + OAuthConfig: config.GoogleOauth, + }, + Outlook: oauth.OutlookOAuthClient{ + OAuthConfig: config.OutlookOauth, + }, + }, } - return integrations } diff --git a/backend/tests/api/helpers/app.go b/backend/tests/api/helpers/app.go index 661f473d5..cea11931f 100644 --- a/backend/tests/api/helpers/app.go +++ b/backend/tests/api/helpers/app.go @@ -33,7 +33,7 @@ func spawnApp() (*TestApp, error) { configuration.Database.DatabaseName = generateRandomDBName() - connectionWithDB, err := configureDatabase(*configuration) + connectionWithDB, err := configureDatabase(configuration.Database, configuration.SuperUser) if err != nil { return nil, err } diff --git a/backend/tests/api/helpers/db.go b/backend/tests/api/helpers/db.go index fbda2250a..434c15846 100644 --- a/backend/tests/api/helpers/db.go +++ b/backend/tests/api/helpers/db.go @@ -29,15 +29,15 @@ func RootConn(dbSettings config.DatabaseSettings) { }) } -func configureDatabase(settings config.Settings) (*gorm.DB, error) { - RootConn(settings.Database) +func configureDatabase(databaseSettings config.DatabaseSettings, superUserSettings config.SuperUserSettings) (*gorm.DB, error) { + RootConn(databaseSettings) - err := rootConn.Exec(fmt.Sprintf("CREATE DATABASE %s", settings.Database.DatabaseName)).Error + err := rootConn.Exec(fmt.Sprintf("CREATE DATABASE %s", databaseSettings.DatabaseName)).Error if err != nil { return nil, err } - dbWithDB, err := database.EstablishConn(settings.Database.WithDb()) + dbWithDB, err := database.EstablishConn(databaseSettings.WithDb()) if err != nil { return nil, err } @@ -64,7 +64,7 @@ func configureDatabase(settings config.Settings) (*gorm.DB, error) { return nil, err } - err = database.CreateSuperUserIfNotExists(settings, dbWithDB) + err = database.CreateSuperUserIfNotExists(superUserSettings, dbWithDB) if err != nil { return nil, err }