Skip to content

Commit

Permalink
🧹 chore: config reorg and exported vs private
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed May 28, 2024
1 parent d40e2ed commit a22d4f5
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 97 deletions.
10 changes: 7 additions & 3 deletions backend/config/auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import m "github.com/garrettladley/mattress"
import (
"fmt"

m "github.com/garrettladley/mattress"
)

type AuthSettings struct {
AccessKey *m.Secret[string]
Expand All @@ -15,12 +19,12 @@ type intermediateAuthSettings struct {
func (i *intermediateAuthSettings) into() (*AuthSettings, error) {
accessKey, err := m.NewSecret(i.AccessKey)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create secret from access key: %s", err.Error())
}

refreshKey, err := m.NewSecret(i.RefreshKey)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create secret from refresh key: %s", err.Error())
}

return &AuthSettings{
Expand Down
14 changes: 9 additions & 5 deletions backend/config/aws.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import m "github.com/garrettladley/mattress"
import (
"fmt"

m "github.com/garrettladley/mattress"
)

type AWSSettings struct {
BucketName *m.Secret[string]
Expand All @@ -19,22 +23,22 @@ type intermediateAWSSettings struct {
func (i *intermediateAWSSettings) into() (*AWSSettings, error) {
bucketName, err := m.NewSecret(i.BucketName)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create secret from bucket name: %s", err.Error())
}

id, err := m.NewSecret(i.Id)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create secret from ID: %s", err.Error())
}

secret, err := m.NewSecret(i.Secret)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create secret from secret: %s", err.Error())
}

region, err := m.NewSecret(i.Region)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create secret from region: %s", err.Error())
}

return &AWSSettings{
Expand Down
11 changes: 7 additions & 4 deletions backend/config/calendar.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import "time"
import (
"fmt"
"time"
)

type CalendarSettings struct {
MaxTerminationDate time.Time
Expand All @@ -11,12 +14,12 @@ type intermediateCalendarSettings struct {
}

func (i *intermediateCalendarSettings) into() (*CalendarSettings, error) {
t, err := time.Parse("01-02-2006", i.MaxTerminationDate)
maxTerminationDate, err := time.Parse("01-02-2006", i.MaxTerminationDate)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to parse max termination date: %s", err.Error())
}

return &CalendarSettings{
MaxTerminationDate: t,
MaxTerminationDate: maxTerminationDate,
}, nil
}
6 changes: 4 additions & 2 deletions backend/config/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package config

import (
"fmt"

"github.com/caarlos0/env/v11"
"github.com/joho/godotenv"
)

func GetConfiguration(path string) (*Settings, error) {
err := godotenv.Load(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to load environment variables: %s", err.Error())
}

var intermediateSettings intermediateSettings
if err := env.Parse(&intermediateSettings); err != nil {
return nil, err
return nil, fmt.Errorf("failed to parse environment variables: %s", err.Error())
}

settings, err := intermediateSettings.into()
Expand Down
2 changes: 1 addition & 1 deletion backend/config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type intermediateDatabaseSettings struct {
func (i *intermediateDatabaseSettings) into() (*DatabaseSettings, error) {
password, err := m.NewSecret(i.Password)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create secret from password: %s", err.Error())
}

return &DatabaseSettings{
Expand Down
63 changes: 1 addition & 62 deletions backend/config/oauth.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config

import (
"errors"

m "github.com/garrettladley/mattress"
)

Expand All @@ -20,66 +18,7 @@ type OAuthSettings struct {
Prompt string
}

type OAuthResources struct {
type OauthProviderSettings struct {
GoogleOAuthSettings *OAuthSettings
OutlookOAuthSettings *OAuthSettings
}

type GoogleOAuthSettings struct {
ClientId string `env:"GOOGLE_OAUTH_CLIENT_ID"`
ClientSecret string `env:"GOOGLE_OAUTH_CLIENT_SECRET"`
}

func (i *GoogleOAuthSettings) into() (*OAuthSettings, error) {
secretClientID, err := m.NewSecret(i.ClientId)
if err != nil {
return nil, errors.New("failed to create secret from client ID")
}

secretClientSecret, err := m.NewSecret(i.ClientSecret)
if err != nil {
return nil, errors.New("failed to create secret from client secret")
}

return &OAuthSettings{
BaseURL: "https://accounts.google.com/o/oauth2/v2",
TokenURL: "https://oauth2.googleapis.com",
ClientID: secretClientID,
ClientSecret: secretClientSecret,
Scopes: "https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly",
ResponseType: "code",
RedirectURI: "http://localhost:3000",
IncludeGrantedScopes: "true",
AccessType: "offline",
Prompt: "consent",
}, nil
}

type OutlookOAuthSettings struct {
ClientId string `env:"OUTLOOK_OAUTH_CLIENT_ID"`
ClientSecret string `env:"OUTLOOK_OAUTH_CLIENT_SECRET"`
}

func (i *OutlookOAuthSettings) into() (*OAuthSettings, error) {
secretClientID, err := m.NewSecret(i.ClientId)
if err != nil {
return nil, errors.New("failed to create secret from client ID")
}

secretClientSecret, err := m.NewSecret(i.ClientSecret)
if err != nil {
return nil, errors.New("failed to create secret from client secret")
}

return &OAuthSettings{
BaseURL: "https://login.microsoftonline.com/common/oauth2/v2.0",
TokenURL: "https://login.microsoftonline.com/common/oauth2/v2.0",
ClientID: secretClientID,
ClientSecret: secretClientSecret,
Scopes: "offline_access user.read calendars.readwrite",
ResponseType: "code",
RedirectURI: "http://localhost:3000",
ResponseMode: "query",
Prompt: "consent",
}, nil
}
37 changes: 37 additions & 0 deletions backend/config/oauth_google.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

import (
"fmt"

m "github.com/garrettladley/mattress"
)

type GoogleOAuthSettings struct {
ClientId string `env:"GOOGLE_OAUTH_CLIENT_ID"`
ClientSecret string `env:"GOOGLE_OAUTH_CLIENT_SECRET"`
}

func (i *GoogleOAuthSettings) into() (*OAuthSettings, error) {
secretClientID, err := m.NewSecret(i.ClientId)
if err != nil {
return nil, fmt.Errorf("failed to create secret from client ID: %s", err.Error())
}

secretClientSecret, err := m.NewSecret(i.ClientSecret)
if err != nil {
return nil, fmt.Errorf("failed to create secret from client secret: %s", err.Error())
}

return &OAuthSettings{
BaseURL: "https://accounts.google.com/o/oauth2/v2",
TokenURL: "https://oauth2.googleapis.com",
ClientID: secretClientID,
ClientSecret: secretClientSecret,
Scopes: "https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly",
ResponseType: "code",
RedirectURI: "http://localhost:3000",
IncludeGrantedScopes: "true",
AccessType: "offline",
Prompt: "consent",
}, nil
}
36 changes: 36 additions & 0 deletions backend/config/oauth_outlook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package config

import (
"fmt"

m "github.com/garrettladley/mattress"
)

type OutlookOAuthSettings struct {
ClientId string `env:"OUTLOOK_OAUTH_CLIENT_ID"`
ClientSecret string `env:"OUTLOOK_OAUTH_CLIENT_SECRET"`
}

func (i *OutlookOAuthSettings) into() (*OAuthSettings, error) {
secretClientID, err := m.NewSecret(i.ClientId)
if err != nil {
return nil, fmt.Errorf("failed to create secret from client ID: %s", err.Error())
}

secretClientSecret, err := m.NewSecret(i.ClientSecret)
if err != nil {
return nil, fmt.Errorf("failed to create secret from client secret: %s", err.Error())
}

return &OAuthSettings{
BaseURL: "https://login.microsoftonline.com/common/oauth2/v2.0",
TokenURL: "https://login.microsoftonline.com/common/oauth2/v2.0",
ClientID: secretClientID,
ClientSecret: secretClientSecret,
Scopes: "offline_access user.read calendars.readwrite",
ResponseType: "code",
RedirectURI: "http://localhost:3000",
ResponseMode: "query",
Prompt: "consent",
}, nil
}
34 changes: 17 additions & 17 deletions backend/config/redis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"errors"
"fmt"

m "github.com/garrettladley/mattress"
)
Expand All @@ -15,22 +15,6 @@ type RedisSettings struct {
// TLSConfig *TLSConfig
}

func (int *intermediateRedisSettings) into() (*RedisSettings, error) {
password, err := m.NewSecret(int.Password)
if err != nil {
return nil, errors.New("failed to create secret from password")
}

return &RedisSettings{
Username: int.Username,
Password: password,
Host: int.Host,
Port: int.Port,
DB: int.DB,
// TLSConfig: int.TLSConfig.into(),
}, nil
}

type intermediateRedisSettings struct {
Username string `env:"USERNAME"`
Password string `env:"PASSWORD"`
Expand All @@ -39,3 +23,19 @@ type intermediateRedisSettings struct {
DB int `env:"DB"`
// TLSConfig *intermediateTLSConfig `env:"TLS_CONFIG"`
}

func (i *intermediateRedisSettings) into() (*RedisSettings, error) {
password, err := m.NewSecret(i.Password)
if err != nil {
return nil, fmt.Errorf("failed to create secret from password: %s", err.Error())
}

return &RedisSettings{
Username: i.Username,
Password: password,
Host: i.Host,
Port: i.Port,
DB: i.DB,
// TLSConfig: i.TLSConfig.into(),
}, nil
}
2 changes: 1 addition & 1 deletion backend/integrations/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
type Integrations struct {
Email email.EmailClientInterface
File file.FileClientInterface
OAuth oauth.OAuthResources
OAuth oauth.OauthProviderSettings
}
2 changes: 1 addition & 1 deletion backend/integrations/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type OAuthClient struct {
ResourceClient OAuthResourceClientInterface
}

type OAuthResources struct {
type OauthProviderSettings struct {
Outlook OutlookOAuthClient
Google GoogleOAuthClient
}
2 changes: 1 addition & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func configureIntegrations(config *config.Settings) integrations.Integrations {
OAuthConfig: config.Outlook,
}

oauthResources := oauth.OAuthResources{
oauthResources := oauth.OauthProviderSettings{
Google: googleClient,
Outlook: outlookClient,
}
Expand Down

0 comments on commit a22d4f5

Please sign in to comment.