diff --git a/config/smtp.go b/config/smtp.go index e4ca0f8..9ec799f 100644 --- a/config/smtp.go +++ b/config/smtp.go @@ -5,7 +5,7 @@ import ( "fmt" ) -// mandatory parameters for the STMP server connection +// SMTPConfig handles mandatory parameters for the STMP server connection type SMTPConfig struct { User string Password string diff --git a/mail/mail.go b/mail/mail.go index 37e0e93..dfd8bf6 100644 --- a/mail/mail.go +++ b/mail/mail.go @@ -40,10 +40,10 @@ func FromJSON(path string) (Email, error) { func (e Email) Generate() []byte { mail := fmt.Sprintf( - "To: %s\nFrom: %s\nContent-Type: text/html\nSubject: %s\n\n%s", + "To: %s\nFrom: %s\nContent-Type: text/html;charset=utf-8\nSubject: %s\n\n%s", strings.Join(e.Receivers, ","), e.Sender, - e.Sender, + e.Subject, e.Content, ) return []byte(mail) diff --git a/main.go b/main.go index 586f0d9..2c4bbce 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ const ( DefaultOutboxPath string = "outbox" ) -// simply collects binary arguments +// GetConfigPath simply collects binary arguments func GetConfigPath() (string, error) { switch len(os.Args) { case 1: @@ -43,7 +43,7 @@ func LoadIni() (*ini.File, error) { return ini, nil } -// collects mandatory SMTP parameters to send an e-mail from the `.ini` file +// LoadSMTPConfig collects mandatory SMTP parameters to send an e-mail from the `.ini` file func LoadSMTPConfig(iniFile *ini.File) (cfg.SMTPConfig, error) { var config cfg.SMTPConfig @@ -61,7 +61,7 @@ func LoadSMTPConfig(iniFile *ini.File) (cfg.SMTPConfig, error) { return config, nil } -// tries to get the outbox path from the `.ini` file and creates the directory +// GetOutBoxPath tries to get the outbox path from the `.ini` file and creates the directory // if the path does not exist, create a default one: `outbox` next to the binary func GetOutboxPath(iniFile *ini.File) (string, error) { outboxPath := iniFile.Section("service").Key("outbox_path").String() diff --git a/services/sender.go b/services/sender.go index 5029ead..b2cef34 100644 --- a/services/sender.go +++ b/services/sender.go @@ -5,8 +5,10 @@ import ( "mailsrv/mail" "mailsrv/runtime" "os" + "os/signal" "path" "strings" + "syscall" "time" "github.com/go-kit/kit/log" @@ -50,7 +52,7 @@ func (s Sender) SendMail(email mail.Email) error { return nil } -// every `TickerInterval` reads the `outbox` directory and put JSON format e-mail in the queue +// watchOutbox reads the `outbox` directory every `TickInterval` and put JSON format e-mail in the queue func (s Sender) watchOutbox() { ticker := time.NewTicker(TickerInterval) go func() { @@ -75,8 +77,8 @@ func (s Sender) watchOutbox() { }() } -// loops over the queue and send email -func (s Sender) processNextMail() bool { +// processNextEmail loops over the queue and send email +func (s Sender) processNextEmail() bool { item, quit := s.queue.Get() if quit { return false @@ -104,10 +106,32 @@ func (s Sender) processNextMail() bool { return true } +// run starts processing the queue +func (s Sender) run() <-chan struct{} { + queueCh := make(chan struct{}) + go func() { + for s.processNextEmail() { + } + queueCh <- struct{}{} + }() + return queueCh +} + +// Run launches the queue processing and the outbox watcher +// catches `SIGINT` and `SIGTERM` to properly stopped the queue func (s Sender) Run() { s.logger.Log("msg", "sender service is running") + sigCh := make(chan os.Signal, 1) + signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) + s.watchOutbox() - for s.processNextMail() { - } + queueCh := s.run() + + <-sigCh + s.logger.Log("msg", "stop signal received, stopping e-mail queue...") + s.queue.Shutdown() + + <-queueCh + s.logger.Log("msg", "sender service stopped successfully") }