refactor: move config related code into dedicated package
This commit is contained in:
@ -3,31 +3,22 @@ package utils
|
||||
import (
|
||||
"crypto/tls"
|
||||
|
||||
"github.com/jordanknott/taskcafe/internal/config"
|
||||
hermes "github.com/matcornic/hermes/v2"
|
||||
gomail "gopkg.in/mail.v2"
|
||||
)
|
||||
|
||||
type EmailConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
From string
|
||||
Username string
|
||||
Password string
|
||||
SiteURL string
|
||||
InsecureSkipVerify bool
|
||||
}
|
||||
|
||||
type EmailInvite struct {
|
||||
ConfirmToken string
|
||||
FullName string
|
||||
To string
|
||||
}
|
||||
|
||||
func SendEmailInvite(config EmailConfig, invite EmailInvite) error {
|
||||
func SendEmailInvite(cfg config.EmailConfig, invite EmailInvite) error {
|
||||
h := hermes.Hermes{
|
||||
Product: hermes.Product{
|
||||
Name: "Taskscafe",
|
||||
Link: config.SiteURL,
|
||||
Link: cfg.SiteURL,
|
||||
Logo: "https://github.com/JordanKnott/taskcafe/raw/master/.github/taskcafe-full.png",
|
||||
},
|
||||
}
|
||||
@ -45,7 +36,7 @@ func SendEmailInvite(config EmailConfig, invite EmailInvite) error {
|
||||
Color: "#7367F0", // Optional action button color
|
||||
TextColor: "#FFFFFF",
|
||||
Text: "Register your account",
|
||||
Link: config.SiteURL + "/register?confirmToken=" + invite.ConfirmToken,
|
||||
Link: cfg.SiteURL + "/register?confirmToken=" + invite.ConfirmToken,
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -67,7 +58,7 @@ func SendEmailInvite(config EmailConfig, invite EmailInvite) error {
|
||||
m := gomail.NewMessage()
|
||||
|
||||
// Set E-Mail sender
|
||||
m.SetHeader("From", config.From)
|
||||
m.SetHeader("From", cfg.From)
|
||||
|
||||
// Set E-Mail receivers
|
||||
m.SetHeader("To", invite.To)
|
||||
@ -80,11 +71,11 @@ func SendEmailInvite(config EmailConfig, invite EmailInvite) error {
|
||||
m.AddAlternative("text/plain", emailBodyPlain)
|
||||
|
||||
// Settings for SMTP server
|
||||
d := gomail.NewDialer(config.Host, config.Port, config.Username, config.Password)
|
||||
d := gomail.NewDialer(cfg.Host, cfg.Port, cfg.Username, cfg.Password)
|
||||
|
||||
// This is only needed when SSL/TLS certificate is not valid on server.
|
||||
// In production this should be set to false.
|
||||
d.TLSConfig = &tls.Config{InsecureSkipVerify: config.InsecureSkipVerify}
|
||||
d.TLSConfig = &tls.Config{InsecureSkipVerify: cfg.InsecureSkipVerify}
|
||||
|
||||
// Now send E-Mail
|
||||
if err := d.DialAndSend(m); err != nil {
|
||||
|
@ -1,21 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type SecurityConfig struct {
|
||||
AccessTokenExpiration time.Duration
|
||||
Secret []byte
|
||||
}
|
||||
|
||||
func GetSecurityConfig(accessTokenExp string, secret []byte) (SecurityConfig, error) {
|
||||
exp, err := time.ParseDuration(accessTokenExp)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("issue parsing duration")
|
||||
return SecurityConfig{}, err
|
||||
}
|
||||
return SecurityConfig{AccessTokenExpiration: exp, Secret: secret}, nil
|
||||
}
|
Reference in New Issue
Block a user