105 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			105 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"errors"
 | |
| 	"fmt"
 | |
| 	"os"
 | |
| 
 | |
| 	cfg "mailsrv/config"
 | |
| 	srv "mailsrv/services"
 | |
| 
 | |
| 	"github.com/go-kit/kit/log"
 | |
| 	"github.com/go-kit/kit/log/level"
 | |
| 	ini "gopkg.in/ini.v1"
 | |
| )
 | |
| 
 | |
| const (
 | |
| 	DefaultOutboxPath string = "outbox"
 | |
| )
 | |
| 
 | |
| // GetConfigPath simply collects binary arguments
 | |
| func GetConfigPath() (string, error) {
 | |
| 	switch len(os.Args) {
 | |
| 	case 1:
 | |
| 		return "", errors.New("mailsrv must have .ini config file as first parameter")
 | |
| 	case 2:
 | |
| 		return os.Args[1], nil
 | |
| 	default:
 | |
| 		return "", errors.New("mailsrv must only have one parameter: .ini path file")
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func LoadIni() (*ini.File, error) {
 | |
| 	configPath, err := GetConfigPath()
 | |
| 	if err != nil {
 | |
| 		return nil, fmt.Errorf("unable to get the .ini config path err=%v", err)
 | |
| 	}
 | |
| 
 | |
| 	ini, err := ini.Load(configPath)
 | |
| 	if err != nil {
 | |
| 		return nil, fmt.Errorf("unable to load the .ini config path err=%v", err)
 | |
| 	}
 | |
| 
 | |
| 	return ini, nil
 | |
| }
 | |
| 
 | |
| // 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
 | |
| 
 | |
| 	section := iniFile.Section("server")
 | |
| 	config, err := cfg.NewSMTPConfig(
 | |
| 		section.Key("username").String(),
 | |
| 		section.Key("password").String(),
 | |
| 		section.Key("url").String(),
 | |
| 		section.Key("port").String(),
 | |
| 	)
 | |
| 	if err != nil {
 | |
| 		return config, fmt.Errorf("failed to load the SMTP configuration err=%v", err)
 | |
| 	}
 | |
| 
 | |
| 	return config, nil
 | |
| }
 | |
| 
 | |
| // 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()
 | |
| 	if outboxPath == "" {
 | |
| 		outboxPath = DefaultOutboxPath
 | |
| 	}
 | |
| 
 | |
| 	if err := os.MkdirAll(outboxPath, 0750); err != nil && !os.IsExist(err) {
 | |
| 		return "", err
 | |
| 	}
 | |
| 
 | |
| 	return outboxPath, nil
 | |
| }
 | |
| 
 | |
| func main() {
 | |
| 	logger := log.NewLogfmtLogger(os.Stdout)
 | |
| 	logger = level.NewFilter(logger, level.AllowInfo())
 | |
| 	logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller, "service", "mailsrv")
 | |
| 
 | |
| 	iniFile, err := LoadIni()
 | |
| 	if err != nil {
 | |
| 		level.Error(logger).Log("msg", "unable to load the .ini configuration file", "err", err)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	config, err := LoadSMTPConfig(iniFile)
 | |
| 	if err != nil {
 | |
| 		level.Error(logger).Log("msg", "unable to load the SMTP configuration", "err", err)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	outboxPath, err := GetOutboxPath(iniFile)
 | |
| 	if err != nil {
 | |
| 		level.Error(logger).Log("msg", "unable to retrieve outputbox path", "err", err)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	sender := srv.NewSender(logger, config, outboxPath)
 | |
| 	sender.Run()
 | |
| }
 |