Fix the mail configuration

This commit is contained in:
Melon 2025-01-11 00:38:01 +00:00
parent 196fd74d45
commit 6d09792e59
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
4 changed files with 14 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/1f349/lavender/conf" "github.com/1f349/lavender/conf"
"github.com/1f349/lavender/database" "github.com/1f349/lavender/database"
"github.com/1f349/lavender/logger" "github.com/1f349/lavender/logger"
"github.com/1f349/lavender/mail"
"github.com/1f349/lavender/role" "github.com/1f349/lavender/role"
"github.com/1f349/lavender/server" "github.com/1f349/lavender/server"
"github.com/1f349/lavender/web" "github.com/1f349/lavender/web"
@ -96,6 +97,11 @@ func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{})
} }
wd := filepath.Dir(configPathAbs) wd := filepath.Dir(configPathAbs)
mailSender, err := mail.New(&config.Mail, wd, "")
if err != nil {
logger.Logger.Fatal("Failed to create mail sender", "err", err)
}
// load the keystore private and public keys // load the keystore private and public keys
keyDir := filepath.Join(wd, "keystore") keyDir := filepath.Join(wd, "keystore")
err = os.MkdirAll(keyDir, 0700) err = os.MkdirAll(keyDir, 0700)
@ -131,7 +137,7 @@ func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{})
} }
mux := httprouter.New() mux := httprouter.New()
server.SetupRouter(mux, config, db, signingKey) server.SetupRouter(mux, config, mailSender, db, signingKey)
srv := &http.Server{ srv := &http.Server{
Handler: mux, Handler: mux,
ReadTimeout: time.Minute, ReadTimeout: time.Minute,

View File

@ -2,7 +2,7 @@ package conf
import ( import (
"github.com/1f349/lavender/issuer" "github.com/1f349/lavender/issuer"
"github.com/1f349/lavender/mail" "github.com/1f349/simplemail"
) )
type Conf struct { type Conf struct {
@ -13,6 +13,6 @@ type Conf struct {
Kid string `yaml:"kid"` Kid string `yaml:"kid"`
Namespace string `yaml:"namespace"` Namespace string `yaml:"namespace"`
OtpIssuer string `yaml:"otpIssuer"` OtpIssuer string `yaml:"otpIssuer"`
Mail mail.Mail `yaml:"mail"` Mail simplemail.Mail `yaml:"mail"`
SsoServices []issuer.SsoConfig `yaml:"ssoServices"` SsoServices []issuer.SsoConfig `yaml:"ssoServices"`
} }

View File

@ -113,7 +113,7 @@ func (h *httpServer) MailDelete(rw http.ResponseWriter, req *http.Request, param
return return
} }
err = h.conf.Mail.SendEmailTemplate("mail-account-delete", "Account Deletion", userInfo.Name, address, nil) err = h.mailSender.SendEmailTemplate("mail-account-delete", "Account Deletion", userInfo.Name, address, nil)
if err != nil { if err != nil {
http.Error(rw, "Failed to send confirmation email.", http.StatusInternalServerError) http.Error(rw, "Failed to send confirmation email.", http.StatusInternalServerError)
return return

View File

@ -9,6 +9,7 @@ import (
"github.com/1f349/lavender/database" "github.com/1f349/lavender/database"
"github.com/1f349/lavender/issuer" "github.com/1f349/lavender/issuer"
"github.com/1f349/lavender/logger" "github.com/1f349/lavender/logger"
"github.com/1f349/lavender/mail"
"github.com/1f349/lavender/web" "github.com/1f349/lavender/web"
"github.com/1f349/mjwt" "github.com/1f349/mjwt"
"github.com/go-oauth2/oauth2/v4/manage" "github.com/go-oauth2/oauth2/v4/manage"
@ -28,6 +29,7 @@ type httpServer struct {
db *database.Queries db *database.Queries
conf conf.Conf conf conf.Conf
signingKey *mjwt.Issuer signingKey *mjwt.Issuer
mailSender *mail.Mail
manager *issuer.Manager manager *issuer.Manager
// mailLinkCache contains a mapping of verify uuids to user uuids // mailLinkCache contains a mapping of verify uuids to user uuids
@ -53,7 +55,7 @@ type mailLinkKey struct {
data string data string
} }
func SetupRouter(r *httprouter.Router, config conf.Conf, db *database.Queries, signingKey *mjwt.Issuer) { func SetupRouter(r *httprouter.Router, config conf.Conf, mailSender *mail.Mail, db *database.Queries, signingKey *mjwt.Issuer) {
// remove last slash from baseUrl // remove last slash from baseUrl
config.BaseUrl = strings.TrimRight(config.BaseUrl, "/") config.BaseUrl = strings.TrimRight(config.BaseUrl, "/")
@ -67,6 +69,7 @@ func SetupRouter(r *httprouter.Router, config conf.Conf, db *database.Queries, s
db: db, db: db,
conf: config, conf: config,
signingKey: signingKey, signingKey: signingKey,
mailSender: mailSender,
mailLinkCache: cache.New[mailLinkKey, string](), mailLinkCache: cache.New[mailLinkKey, string](),