Skip to content

Commit

Permalink
🧹 chore: clean up config params to only pass necessary settings (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored May 28, 2024
1 parent d9e7766 commit 13b4e60
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
22 changes: 14 additions & 8 deletions backend/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions backend/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down
30 changes: 12 additions & 18 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {

stores := store.ConfigureRedis(*config)

integrations := configureIntegrations(config)
integrations := configureIntegrations(&config.Integrations)

tp := telemetry.InitTracer()

Expand All @@ -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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion backend/tests/api/helpers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions backend/tests/api/helpers/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 13b4e60

Please sign in to comment.