Add template parsing and execution to mail service.
This commit is contained in:
parent
15974c9b77
commit
337ebb67b4
4 changed files with 38 additions and 14 deletions
|
|
@ -37,9 +37,9 @@ func NewContainer() *Container {
|
||||||
c.initCache()
|
c.initCache()
|
||||||
c.initDatabase()
|
c.initDatabase()
|
||||||
c.initORM()
|
c.initORM()
|
||||||
c.initMail()
|
|
||||||
c.initAuth()
|
c.initAuth()
|
||||||
c.initTemplateRenderer()
|
c.initTemplateRenderer()
|
||||||
|
c.initMail()
|
||||||
return c
|
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() {
|
func (c *Container) initAuth() {
|
||||||
c.Auth = NewAuthClient(c.Config, c.ORM)
|
c.Auth = NewAuthClient(c.Config, c.ORM)
|
||||||
}
|
}
|
||||||
|
|
@ -150,3 +142,11 @@ func (c *Container) initAuth() {
|
||||||
func (c *Container) initTemplateRenderer() {
|
func (c *Container) initTemplateRenderer() {
|
||||||
c.Templates = NewTemplateRenderer(c.Config)
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"goweb/config"
|
"goweb/config"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
|
@ -13,12 +15,15 @@ import (
|
||||||
type MailClient struct {
|
type MailClient struct {
|
||||||
// config stores application configuration
|
// config stores application configuration
|
||||||
config *config.Config
|
config *config.Config
|
||||||
|
|
||||||
|
templates *TemplateRenderer
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMailClient creates a new MailClient
|
// NewMailClient creates a new MailClient
|
||||||
func NewMailClient(cfg *config.Config) (*MailClient, error) {
|
func NewMailClient(cfg *config.Config, templates *TemplateRenderer) (*MailClient, error) {
|
||||||
return &MailClient{
|
return &MailClient{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
|
templates: templates,
|
||||||
}, nil
|
}, 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")
|
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
|
// TODO: Finish based on your mail sender of choice
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,9 +47,9 @@ func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer {
|
||||||
func (t *TemplateRenderer) Parse(module, key, name string, files []string, directories []string) error {
|
func (t *TemplateRenderer) Parse(module, key, name string, files []string, directories []string) error {
|
||||||
cacheKey := t.getCacheKey(module, key)
|
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
|
// Check if the template has not yet been parsed or if the app environment is local, so that
|
||||||
// changes without having the restart the server
|
// templates reflect changes without having the restart the server
|
||||||
if _, err := t.Load(module, key); err != nil {
|
if _, err := t.Load(module, key); err != nil || t.config.App.Environment == config.EnvLocal {
|
||||||
// Initialize the parsed template with the function map
|
// Initialize the parsed template with the function map
|
||||||
parsed := template.New(name + config.TemplateExt).
|
parsed := template.New(name + config.TemplateExt).
|
||||||
Funcs(t.funcMap)
|
Funcs(t.funcMap)
|
||||||
|
|
|
||||||
1
templates/emails/test.gohtml
Normal file
1
templates/emails/test.gohtml
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Test email template. See services/mail.go to provide your implementation.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue