diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2021-08-07 20:38:37 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2021-08-07 20:38:37 -0600 |
commit | 0197d9cd493b5785bca05f476856540ec64da64a (patch) | |
tree | db19ac4bfa602b1e0b001769c57d6b7c37d96fc4 /srv/mailinglist/mailinglist.go | |
parent | dce39b836a0fd6e37ab2499c2e0e232572c17ad6 (diff) |
split configuration parsing out into separate packages, split api out as well
Diffstat (limited to 'srv/mailinglist/mailinglist.go')
-rw-r--r-- | srv/mailinglist/mailinglist.go | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/srv/mailinglist/mailinglist.go b/srv/mailinglist/mailinglist.go index 2ebb952..60c1174 100644 --- a/srv/mailinglist/mailinglist.go +++ b/srv/mailinglist/mailinglist.go @@ -4,13 +4,17 @@ package mailinglist import ( "bytes" + "context" "errors" "fmt" "html/template" "io" + "net/url" "strings" "github.com/google/uuid" + "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" + "github.com/mediocregopher/mediocre-go-lib/v2/mctx" "github.com/tilinna/clock" ) @@ -42,13 +46,28 @@ type Params struct { Mailer Mailer Clock clock.Clock - // URL of the page which should be navigated to in order to finalize a - // subscription. - FinalizeSubURL string + // PublicURL is the base URL which site visitors can navigate to. + // MailingList will generate links based on this value. + PublicURL *url.URL +} + +// SetupCfg implement the cfg.Cfger interface. +func (p *Params) SetupCfg(cfg *cfg.Cfg) { + publicURLStr := cfg.String("public-url", "http://localhost:4000", "URL this service is accessible at") + + cfg.OnInit(func(ctx context.Context) error { + var err error + if p.PublicURL, err = url.Parse(*publicURLStr); err != nil { + return fmt.Errorf("parsing -public-url: %w", err) + } + + return nil + }) +} - // URL of the page which should be navigated to in order to remove a - // subscription. - UnsubURL string +// Annotate implements mctx.Annotator interface. +func (p *Params) Annotate(a mctx.Annotations) { + a["publicURL"] = p.PublicURL } // New initializes and returns a MailingList instance using the given Params. @@ -105,7 +124,11 @@ func (m *mailingList) BeginSubscription(email string) error { err = beginSubTpl.Execute(body, struct { SubLink string }{ - SubLink: fmt.Sprintf("%s?subToken=%s", m.params.FinalizeSubURL, emailRecord.SubToken), + SubLink: fmt.Sprintf( + "%s/mailinglist/finalize.html?subToken=%s", + m.params.PublicURL.String(), + emailRecord.SubToken, + ), }) if err != nil { @@ -217,7 +240,11 @@ func (m *mailingList) Publish(postTitle, postURL string) error { }{ PostTitle: postTitle, PostURL: postURL, - UnsubURL: fmt.Sprintf("%s?unsubToken=%s", m.params.UnsubURL, emailRecord.UnsubToken), + UnsubURL: fmt.Sprintf( + "%s/mailinglist/unsubscribe.html?unsubToken=%s", + m.params.PublicURL.String(), + emailRecord.UnsubToken, + ), }) if err != nil { |