Add template parsing and execution to mail service.

This commit is contained in:
mikestefanello 2021-12-19 20:37:51 -05:00
parent 15974c9b77
commit 337ebb67b4
4 changed files with 38 additions and 14 deletions

View File

@ -37,9 +37,9 @@ func NewContainer() *Container {
c.initCache()
c.initDatabase()
c.initORM()
c.initMail()
c.initAuth()
c.initTemplateRenderer()
c.initMail()
return c
}
@ -135,14 +135,6 @@ func (c *Container) initORM() {
}
}
func (c *Container) initMail() {
var err error
c.Mail, err = NewMailClient(c.Config)
if err != nil {
panic(fmt.Sprintf("failed to create mail client: %v", err))
}
}
func (c *Container) initAuth() {
c.Auth = NewAuthClient(c.Config, c.ORM)
}
@ -150,3 +142,11 @@ func (c *Container) initAuth() {
func (c *Container) initTemplateRenderer() {
c.Templates = NewTemplateRenderer(c.Config)
}
func (c *Container) initMail() {
var err error
c.Mail, err = NewMailClient(c.Config, c.Templates)
if err != nil {
panic(fmt.Sprintf("failed to create mail client: %v", err))
}
}

View File

@ -1,6 +1,8 @@
package services
import (
"fmt"
"goweb/config"
"github.com/labstack/echo/v4"
@ -13,12 +15,15 @@ import (
type MailClient struct {
// config stores application configuration
config *config.Config
templates *TemplateRenderer
}
// NewMailClient creates a new MailClient
func NewMailClient(cfg *config.Config) (*MailClient, error) {
func NewMailClient(cfg *config.Config, templates *TemplateRenderer) (*MailClient, error) {
return &MailClient{
config: cfg,
templates: templates,
}, nil
}
@ -40,6 +45,24 @@ func (c *MailClient) SendTemplate(ctx echo.Context, to, template string, data in
ctx.Logger().Debugf("skipping template email sent to: %s")
}
// Parse the template, if needed
if err := c.templates.Parse(
"mail",
template,
template,
[]string{fmt.Sprintf("email/%s", template)},
[]string{},
); err != nil {
return err
}
// Execute the template
// Uncomment the first variable when ready to use
_, err := c.templates.Execute("mail", template, template, data)
if err != nil {
return err
}
// TODO: Finish based on your mail sender of choice
return nil
}

View File

@ -47,9 +47,9 @@ func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer {
func (t *TemplateRenderer) Parse(module, key, name string, files []string, directories []string) error {
cacheKey := t.getCacheKey(module, key)
// Check if the template has not yet been parsed or if the app environment is local, so that templates reflect
// changes without having the restart the server
if _, err := t.Load(module, key); err != nil {
// Check if the template has not yet been parsed or if the app environment is local, so that
// templates reflect changes without having the restart the server
if _, err := t.Load(module, key); err != nil || t.config.App.Environment == config.EnvLocal {
// Initialize the parsed template with the function map
parsed := template.New(name + config.TemplateExt).
Funcs(t.funcMap)

View File

@ -0,0 +1 @@
Test email template. See services/mail.go to provide your implementation.