refactor: configuration into separate file

This commit is contained in:
Felipe M 2024-11-24 12:08:35 +01:00
parent f1733d3670
commit c829408095
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8
2 changed files with 57 additions and 35 deletions

46
config.go Normal file
View file

@ -0,0 +1,46 @@
package smtp2shoutrrr
import (
"log/slog"
"net/url"
)
type Config struct {
Port int
Username string
Password string
Recipients []ConfigRecipient
}
func (c *Config) SetDefaults() {
if c.Port == 0 {
c.Port = 11125
}
if c.Username == "" {
slog.Warn("no username provided, using default: username")
c.Username = "username"
}
if c.Password == "" {
slog.Warn("no password provided, using default: password")
c.Password = "password"
}
}
type ConfigRecipient struct {
Addresses []string // email addresses
Target string // shoutrrr address
targetURL *url.URL
}
func (cr ConfigRecipient) GetTargetURL() *url.URL {
if cr.targetURL == nil {
var err error
cr.targetURL, err = url.Parse(cr.Target)
if err != nil {
slog.Error("failed to parse shoutrrr target URL", slog.String("target", cr.Target), slog.String("err", err.Error()))
}
}
return cr.targetURL
}