Initial commit: Open sourcing all of the Maple Open Technologies code.
This commit is contained in:
commit
755d54a99d
2010 changed files with 448675 additions and 0 deletions
61
cloud/maplepress-backend/pkg/emailer/mailgun/config.go
Normal file
61
cloud/maplepress-backend/pkg/emailer/mailgun/config.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package mailgun
|
||||
|
||||
type MailgunConfigurationProvider interface {
|
||||
GetSenderEmail() string
|
||||
GetDomainName() string // Deprecated
|
||||
GetBackendDomainName() string
|
||||
GetFrontendDomainName() string
|
||||
GetMaintenanceEmail() string
|
||||
GetAPIKey() string
|
||||
GetAPIBase() string
|
||||
}
|
||||
|
||||
type mailgunConfigurationProviderImpl struct {
|
||||
senderEmail string
|
||||
domain string
|
||||
apiBase string
|
||||
maintenanceEmail string
|
||||
frontendDomain string
|
||||
backendDomain string
|
||||
apiKey string
|
||||
}
|
||||
|
||||
func NewMailgunConfigurationProvider(senderEmail, domain, apiBase, maintenanceEmail, frontendDomain, backendDomain, apiKey string) MailgunConfigurationProvider {
|
||||
return &mailgunConfigurationProviderImpl{
|
||||
senderEmail: senderEmail,
|
||||
domain: domain,
|
||||
apiBase: apiBase,
|
||||
maintenanceEmail: maintenanceEmail,
|
||||
frontendDomain: frontendDomain,
|
||||
backendDomain: backendDomain,
|
||||
apiKey: apiKey,
|
||||
}
|
||||
}
|
||||
|
||||
func (me *mailgunConfigurationProviderImpl) GetDomainName() string {
|
||||
return me.domain
|
||||
}
|
||||
|
||||
func (me *mailgunConfigurationProviderImpl) GetSenderEmail() string {
|
||||
return me.senderEmail
|
||||
}
|
||||
|
||||
func (me *mailgunConfigurationProviderImpl) GetBackendDomainName() string {
|
||||
return me.backendDomain
|
||||
}
|
||||
|
||||
func (me *mailgunConfigurationProviderImpl) GetFrontendDomainName() string {
|
||||
return me.frontendDomain
|
||||
}
|
||||
|
||||
func (me *mailgunConfigurationProviderImpl) GetMaintenanceEmail() string {
|
||||
return me.maintenanceEmail
|
||||
}
|
||||
|
||||
func (me *mailgunConfigurationProviderImpl) GetAPIKey() string {
|
||||
return me.apiKey
|
||||
}
|
||||
|
||||
func (me *mailgunConfigurationProviderImpl) GetAPIBase() string {
|
||||
return me.apiBase
|
||||
}
|
||||
12
cloud/maplepress-backend/pkg/emailer/mailgun/interface.go
Normal file
12
cloud/maplepress-backend/pkg/emailer/mailgun/interface.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package mailgun
|
||||
|
||||
import "context"
|
||||
|
||||
type Emailer interface {
|
||||
Send(ctx context.Context, sender, subject, recipient, htmlContent string) error
|
||||
GetSenderEmail() string
|
||||
GetDomainName() string // Deprecated
|
||||
GetBackendDomainName() string
|
||||
GetFrontendDomainName() string
|
||||
GetMaintenanceEmail() string
|
||||
}
|
||||
86
cloud/maplepress-backend/pkg/emailer/mailgun/mailgun.go
Normal file
86
cloud/maplepress-backend/pkg/emailer/mailgun/mailgun.go
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
package mailgun
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/mailgun/mailgun-go/v4"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type mailgunEmailer struct {
|
||||
config MailgunConfigurationProvider
|
||||
logger *zap.Logger
|
||||
Mailgun *mailgun.MailgunImpl
|
||||
}
|
||||
|
||||
func NewEmailer(config MailgunConfigurationProvider, logger *zap.Logger) Emailer {
|
||||
logger = logger.Named("mailgun-emailer")
|
||||
|
||||
// Initialize Mailgun client
|
||||
mg := mailgun.NewMailgun(config.GetDomainName(), config.GetAPIKey())
|
||||
mg.SetAPIBase(config.GetAPIBase()) // Override to support our custom email requirements.
|
||||
|
||||
logger.Info("✓ Mailgun emailer initialized",
|
||||
zap.String("domain", config.GetDomainName()),
|
||||
zap.String("api_base", config.GetAPIBase()))
|
||||
|
||||
return &mailgunEmailer{
|
||||
config: config,
|
||||
logger: logger,
|
||||
Mailgun: mg,
|
||||
}
|
||||
}
|
||||
|
||||
func (me *mailgunEmailer) Send(ctx context.Context, sender, subject, recipient, body string) error {
|
||||
me.logger.Debug("Sending email",
|
||||
zap.String("sender", sender),
|
||||
zap.String("recipient", recipient),
|
||||
zap.String("subject", subject))
|
||||
|
||||
message := me.Mailgun.NewMessage(sender, subject, "", recipient)
|
||||
message.SetHtml(body)
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
// Send the message with a 10 second timeout
|
||||
resp, id, err := me.Mailgun.Send(ctx, message)
|
||||
|
||||
if err != nil {
|
||||
me.logger.Error("Failed to send email",
|
||||
zap.String("sender", sender),
|
||||
zap.String("recipient", recipient),
|
||||
zap.String("subject", subject),
|
||||
zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
me.logger.Info("Email sent successfully",
|
||||
zap.String("recipient", recipient),
|
||||
zap.String("subject", subject),
|
||||
zap.String("message_id", id),
|
||||
zap.String("response", resp))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (me *mailgunEmailer) GetDomainName() string {
|
||||
return me.config.GetDomainName()
|
||||
}
|
||||
|
||||
func (me *mailgunEmailer) GetSenderEmail() string {
|
||||
return me.config.GetSenderEmail()
|
||||
}
|
||||
|
||||
func (me *mailgunEmailer) GetBackendDomainName() string {
|
||||
return me.config.GetBackendDomainName()
|
||||
}
|
||||
|
||||
func (me *mailgunEmailer) GetFrontendDomainName() string {
|
||||
return me.config.GetFrontendDomainName()
|
||||
}
|
||||
|
||||
func (me *mailgunEmailer) GetMaintenanceEmail() string {
|
||||
return me.config.GetMaintenanceEmail()
|
||||
}
|
||||
26
cloud/maplepress-backend/pkg/emailer/mailgun/provider.go
Normal file
26
cloud/maplepress-backend/pkg/emailer/mailgun/provider.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// File Path: monorepo/cloud/maplepress-backend/pkg/emailer/mailgun/provider.go
|
||||
package mailgun
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
|
||||
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/config"
|
||||
)
|
||||
|
||||
// ProvideMailgunConfigurationProvider creates a new Mailgun configuration provider from the application config.
|
||||
func ProvideMailgunConfigurationProvider(cfg *config.Config) MailgunConfigurationProvider {
|
||||
return NewMailgunConfigurationProvider(
|
||||
cfg.Mailgun.SenderEmail,
|
||||
cfg.Mailgun.Domain,
|
||||
cfg.Mailgun.APIBase,
|
||||
cfg.Mailgun.MaintenanceEmail,
|
||||
cfg.Mailgun.FrontendDomain,
|
||||
cfg.Mailgun.BackendDomain,
|
||||
cfg.Mailgun.APIKey,
|
||||
)
|
||||
}
|
||||
|
||||
// ProvideEmailer creates a new Mailgun emailer from the configuration provider.
|
||||
func ProvideEmailer(config MailgunConfigurationProvider, logger *zap.Logger) Emailer {
|
||||
return NewEmailer(config, logger)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue