1
0

Better config handling with constants (#83)

This commit is contained in:
konrad
2019-07-06 20:12:26 +00:00
committed by Gitea
parent f1d21ea52b
commit 1f1a079fd3
17 changed files with 200 additions and 120 deletions

View File

@ -17,9 +17,9 @@
package mail
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
"crypto/tls"
"github.com/spf13/viper"
"gopkg.in/gomail.v2"
"time"
)
@ -29,20 +29,20 @@ var Queue chan *gomail.Message
// StartMailDaemon starts the mail daemon
func StartMailDaemon() {
Queue = make(chan *gomail.Message, viper.GetInt("mailer.queuelength"))
Queue = make(chan *gomail.Message, config.MailerQueuelength.GetInt())
if !viper.GetBool("mailer.enabled") {
if !config.MailerEnabled.GetBool() {
return
}
if viper.GetString("mailer.host") == "" {
if config.MailerHost.GetString() == "" {
log.Log.Warning("Mailer seems to be not configured! Please see the config docs for more details.")
return
}
go func() {
d := gomail.NewDialer(viper.GetString("mailer.host"), viper.GetInt("mailer.port"), viper.GetString("mailer.username"), viper.GetString("mailer.password"))
d.TLSConfig = &tls.Config{InsecureSkipVerify: viper.GetBool("mailer.skiptlsverify")}
d := gomail.NewDialer(config.MailerHost.GetString(), config.MailerPort.GetInt(), config.MailerUsername.GetString(), config.MailerPassword.GetString())
d.TLSConfig = &tls.Config{InsecureSkipVerify: config.MailerSkipTLSVerify.GetBool()}
var s gomail.SendCloser
var err error
@ -64,7 +64,7 @@ func StartMailDaemon() {
}
// Close the connection to the SMTP server if no email was sent in
// the last 30 seconds.
case <-time.After(viper.GetDuration("mailer.queuetimeout") * time.Second):
case <-time.After(config.MailerQueueTimeout.GetDuration() * time.Second):
if open {
if err := s.Close(); err != nil {
log.Log.Error("Error closing the mail server connection: %s\n", err)

View File

@ -18,9 +18,9 @@ package mail
import (
"bytes"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/utils"
"github.com/labstack/gommon/log"
"github.com/spf13/viper"
"gopkg.in/gomail.v2"
"text/template"
)
@ -54,7 +54,7 @@ type header struct {
// SendMail puts a mail in the queue
func SendMail(opts *Opts) {
m := gomail.NewMessage()
m.SetHeader("From", viper.GetString("mailer.fromemail"))
m.SetHeader("From", config.MailerFromEmail.GetString())
m.SetHeader("To", opts.To)
m.SetHeader("Subject", opts.Subject)
for _, h := range opts.Headers {
@ -85,13 +85,13 @@ func SendMailWithTemplate(to, subject, tpl string, data map[string]interface{})
var plainContent bytes.Buffer
t := &Template{
Templates: template.Must(template.ParseGlob(viper.GetString("service.rootpath") + "/templates/mail/*.tmpl")),
Templates: template.Must(template.ParseGlob(config.ServiceRootpath.GetString() + "/templates/mail/*.tmpl")),
}
boundary := "np" + utils.MakeRandomString(13)
data["Boundary"] = boundary
data["FrontendURL"] = viper.GetString("service.frontendurl")
data["FrontendURL"] = config.ServiceFrontendurl.GetString()
if err := t.Templates.ExecuteTemplate(&htmlContent, tpl+".html.tmpl", data); err != nil {
log.Error(3, "Template: %v", err)