refactor: make mail init a lifecycle hook so it's called after initCheckRequiredConfig
This commit is contained in:
parent
33af108d39
commit
f89be1fef8
|
@ -1,6 +1,8 @@
|
||||||
package mailer
|
package mailer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"git.lumeweb.com/LumeWeb/portal/config"
|
"git.lumeweb.com/LumeWeb/portal/config"
|
||||||
"github.com/wneessen/go-mail"
|
"github.com/wneessen/go-mail"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
|
@ -41,7 +43,11 @@ func (m *Mailer) TemplateSend(template string, subjectVars TemplateData, bodyVar
|
||||||
return m.client.DialAndSend(msg)
|
return m.client.DialAndSend(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMailer(config *config.Manager, templateRegistry *TemplateRegistry, logger *zap.Logger) (*Mailer, error) {
|
func NewMailer(lc fx.Lifecycle, config *config.Manager, logger *zap.Logger, templateRegistry *TemplateRegistry) (*Mailer, error) {
|
||||||
|
m := &Mailer{config: config, logger: logger, templateRegistry: templateRegistry}
|
||||||
|
|
||||||
|
lc.Append(fx.Hook{
|
||||||
|
OnStart: func(context.Context) error {
|
||||||
var options []mail.Option
|
var options []mail.Option
|
||||||
|
|
||||||
if config.Config().Core.Mail.Port != 0 {
|
if config.Config().Core.Mail.Port != 0 {
|
||||||
|
@ -61,10 +67,13 @@ func NewMailer(config *config.Manager, templateRegistry *TemplateRegistry, logge
|
||||||
|
|
||||||
client, err := mail.NewClient(config.Config().Core.Mail.Host, options...)
|
client, err := mail.NewClient(config.Config().Core.Mail.Host, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m := &Mailer{config: config, logger: logger, client: client, templateRegistry: templateRegistry}
|
m.client = client
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue