better logging in srv when fake Mailer is used

This commit is contained in:
Brian Picciano 2021-08-29 21:34:02 -06:00
parent bf40fa3868
commit 1608ca7425
3 changed files with 22 additions and 3 deletions

View File

@ -1,4 +1,3 @@
all: all:
nix-build -A entrypoint --arg baseConfig '(import ./config.nix) // { staticProxyURL = ""; }' nix-build -A entrypoint --arg baseConfig '(import ./config.nix) // { staticProxyURL = ""; }'

View File

@ -74,8 +74,8 @@ func main() {
var mailer mailinglist.Mailer var mailer mailinglist.Mailer
if mailerParams.SMTPAddr == "" { if mailerParams.SMTPAddr == "" {
logger.Info(ctx, "-smtp-addr not given, using NullMailer") logger.Info(ctx, "-smtp-addr not given, using a fake Mailer")
mailer = mailinglist.NullMailer mailer = mailinglist.NewLogMailer(logger.WithNamespace("fake-mailer"))
} else { } else {
mailer = mailinglist.NewMailer(mailerParams) mailer = mailinglist.NewMailer(mailerParams)
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/emersion/go-smtp" "github.com/emersion/go-smtp"
"github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx" "github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
) )
// Mailer is used to deliver emails to arbitrary recipients. // Mailer is used to deliver emails to arbitrary recipients.
@ -16,6 +17,25 @@ type Mailer interface {
Send(to, subject, body string) error Send(to, subject, body string) error
} }
type logMailer struct {
logger *mlog.Logger
}
// NewLogMailer returns a Mailer instance which will not actually send any
// emails, it will only log to the given Logger when Send is called.
func NewLogMailer(logger *mlog.Logger) Mailer {
return &logMailer{logger: logger}
}
func (l *logMailer) Send(to, subject, body string) error {
ctx := mctx.Annotate(context.Background(),
"to", to,
"subject", subject,
)
l.logger.Info(ctx, "would have sent email")
return nil
}
// NullMailer acts as a Mailer but actually just does nothing. // NullMailer acts as a Mailer but actually just does nothing.
var NullMailer = nullMailer{} var NullMailer = nullMailer{}