Skip to content

Commit

Permalink
Get email sender and region from config
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov committed Jun 28, 2024
1 parent 488d6cb commit 08537d5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/config/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Config struct {
Marketplace MarketplaceConfig `mapstructure:"marketplace"`
DefaultProfiles DefaultProfilesConfig `mapstructure:"default_profiles"`
Crypto CryptoConfig `mapstructure:"crypto"`
Email EmailConfig `mapstructure:"email"`
}

// DefaultConfigForTest returns a configuration with all the struct defaults set,
Expand Down
15 changes: 15 additions & 0 deletions internal/config/server/email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package server

// EmailConfig is the configuration for the email sending service
type EmailConfig struct {
// AWSSES is the AWS SES configuration
AWSSES AWSSES `mapstructure:"aws_ses"`
}

// AWSSES is the AWS SES configuration
type AWSSES struct {
// Sender is the email address of the sender
Sender string `mapstructure:"sender"`
// Region is the AWS region to use for AWS SES
Region string `mapstructure:"region"`
}
18 changes: 14 additions & 4 deletions internal/email/awsses.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
)

const (
// awsUSEast1 is the AWS region
awsUSEast1 = "us-east-1"
// CharSet is the character set for the email
CharSet = "UTF-8"
// DefaultAWSRegion is the default AWS region
DefaultAWSRegion = "us-east-1"
// DefaultSender is the default sender email address
DefaultSender = "[email protected]"
)

// AWSSES is the AWS SES client
Expand All @@ -38,10 +40,18 @@ type AWSSES struct {
}

// NewAWSSES creates a new AWS SES client
func NewAWSSES(sender string) (*AWSSES, error) {
func NewAWSSES(sender, region string) (*AWSSES, error) {
// Set the sender and region in case they are not provided.
if sender == "" {
sender = DefaultSender
}
if region == "" {
region = DefaultAWSRegion
}

// Create a new session.
sess, err := session.NewSession(&aws.Config{
Region: aws.String(awsUSEast1)},
Region: aws.String(region)},
)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ func AllInOneServerService(
evt.ConsumeEvents(im)

// Register the email manager to handle email invitations
// TODO: This should be read from the config
mailClient, err := email.NewAWSSES("[email protected]")
mailClient, err := email.NewAWSSES(cfg.Email.AWSSES.Sender, cfg.Email.AWSSES.Region)
if err != nil {
return fmt.Errorf("unable to create email client: %w", err)
}
Expand Down

0 comments on commit 08537d5

Please sign in to comment.