Skip to content

Commit

Permalink
Merge pull request cozy#1145 from jinroh/mails-dev-helpers
Browse files Browse the repository at this point in the history
Add dev routes to directly test rendering of any mail or template
  • Loading branch information
jinroh authored Jan 3, 2018
2 parents 4e10596 + c5132e5 commit 1d735ab
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/workers/mails/mail_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ func (m *MailTemplater) Execute(name, locale string, recipientName string, data
return
}

// RenderMail returns a rendered mail for the given template name with the
// specified locale, recipient name and template data values.
func RenderMail(name, locale, recipientName string, templateValues interface{}) (string, []*Part, error) {
return mailTemplater.Execute(name, locale, recipientName, templateValues)
}

func init() {
mailTemplater = &MailTemplater{[]*MailTemplate{
{
Expand Down
73 changes: 73 additions & 0 deletions web/dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package web

import (
"fmt"
"net/http"

"github.com/cozy/cozy-stack/pkg/workers/mails"
"github.com/cozy/cozy-stack/web/statik"
"github.com/labstack/echo"
)

// devMailHandler allow to easily render a mail from a route of the stack. The
// query parameters are used as data input for the mail template. The
// ContentType query parameter allow to render the mail in "text/html" or
// "text/plain".
func devMailsHandler(c echo.Context) error {
name := c.Param("name")
locale := statik.GetLanguageFromHeader(c.Request().Header)

recipientName := c.QueryParam("RecipientName")
if recipientName == "" {
recipientName = "Jean Dupont"
}

_, parts, err := mails.RenderMail(name, locale, recipientName, devData(c))
if err != nil {
return err
}

contentType := c.QueryParam("ContentType")
if contentType == "" {
contentType = "text/html"
}

var part *mails.Part
for _, p := range parts {
if p.Type == contentType {
part = p
}
}
if part == nil {
return echo.NewHTTPError(http.StatusNotFound,
fmt.Errorf("Could not find template %q with content-type %q", name, contentType))
}

// Remove all CSP policies to display HTML email. this is a dev-only
// handler, no need to worry.
c.Response().Header().Set(echo.HeaderContentSecurityPolicy, "")
if part.Type == "text/html" {
return c.HTML(http.StatusOK, part.Body)
}
return c.String(http.StatusOK, part.Body)
}

// devTemplatesHandler allow to easily render a given template from a route of
// the stack. The query parameters are used as data input for the template.
func devTemplatesHandler(c echo.Context) error {
name := c.Param("name")
return c.Render(http.StatusOK, name, devData(c))
}

func devData(c echo.Context) echo.Map {
data := make(echo.Map)
for k, v := range c.QueryParams() {
if len(v) > 0 {
data[k] = v[0]
}
}
if _, ok := data["Domain"]; !ok {
data["Domain"] = c.Request().Host
}
return data
}
6 changes: 6 additions & 0 deletions web/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ func SetupRoutes(router *echo.Echo) error {
version.Routes(router.Group("/version"))
}

// dev routes
if config.IsDevRelease() {
router.GET("/dev/mails/:name", devMailsHandler)
router.GET("/dev/templates/:name", devTemplatesHandler)
}

setupRecover(router)
router.HTTPErrorHandler = errors.ErrorHandler
return nil
Expand Down
6 changes: 4 additions & 2 deletions web/statik/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (r *renderer) Render(w io.Writer, name string, data interface{}, c echo.Con
if ok {
funcMap = template.FuncMap{"t": i.Translate}
} else {
lang := getLanguageFromHeader(c.Request().Header)
lang := GetLanguageFromHeader(c.Request().Header)
funcMap = template.FuncMap{"t": i18n.Translator(lang)}
}
t, err := r.t.Clone()
Expand All @@ -147,7 +147,9 @@ func (r *renderer) Render(w io.Writer, name string, data interface{}, c echo.Con
return t.Funcs(funcMap).ExecuteTemplate(w, name, data)
}

func getLanguageFromHeader(header http.Header) (lang string) {
// GetLanguageFromHeader return the language tag given the Accept-Language
// header.
func GetLanguageFromHeader(header http.Header) (lang string) {
// TODO: improve language detection with a package like
// "golang.org/x/text/language"
lang = i18n.DefaultLocale
Expand Down

0 comments on commit 1d735ab

Please sign in to comment.