onasty/mailer/template.go (view raw)
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | ) |
| 7 | |
| 8 | var ErrInvalidTemplate = errors.New("failed to get template") |
| 9 | |
| 10 | type Template struct { |
| 11 | Subject string |
| 12 | Body string |
| 13 | } |
| 14 | |
| 15 | type TemplateFunc func(args map[string]string) Template |
| 16 | |
| 17 | func getTemplate(appURL, frontendURL string, templateName string) (TemplateFunc, error) { |
| 18 | switch templateName { |
| 19 | case "email_verification": |
| 20 | return emailVerificationTemplate(appURL), nil |
| 21 | case "reset_password": |
| 22 | return passwordResetTemplate(frontendURL), nil |
| 23 | default: |
| 24 | return nil, ErrInvalidTemplate |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func emailVerificationTemplate(appURL string) TemplateFunc { |
| 29 | return func(opts map[string]string) Template { |
| 30 | return Template{ |
| 31 | Subject: "Onasty: verify your email", |
| 32 | Body: fmt.Sprintf(`To verify your email, please follow this link: |
| 33 | <a href="%[1]s/api/v1/auth/verify/%[2]s">%[1]s/api/v1/auth/verify/%[2]s</a> |
| 34 | <br /> |
| 35 | <br /> |
| 36 | This link will expire after 24 hours.`, appURL, opts["token"]), |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func passwordResetTemplate(frontendURL string) TemplateFunc { |
| 42 | return func(opts map[string]string) Template { |
| 43 | return Template{ |
| 44 | Subject: "Onasty: reset your password", |
| 45 | Body: fmt.Sprintf(`To reset your password, use this api: |
| 46 | <a href="%[1]s/auth?token=%[2]s">%[1]s/auth?token=%[2]s</a> |
| 47 | <br /> |
| 48 | <br /> |
| 49 | This link will expire after an hour.`, frontendURL, opts["token"]), |
| 50 | } |
| 51 | } |
| 52 | } |