github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/dev.go (about)

     1  package web
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/cozy/cozy-stack/model/instance/lifecycle"
     8  	"github.com/cozy/cozy-stack/model/job"
     9  	"github.com/cozy/cozy-stack/pkg/config/config"
    10  	"github.com/cozy/cozy-stack/pkg/mail"
    11  	"github.com/cozy/cozy-stack/web/middlewares"
    12  	"github.com/cozy/cozy-stack/web/statik"
    13  	"github.com/cozy/cozy-stack/worker/mails"
    14  	"github.com/labstack/echo/v4"
    15  )
    16  
    17  // devMailHandler allow to easily render a mail from a route of the stack. The
    18  // query parameters are used as data input for the mail template. The
    19  // ContentType query parameter allow to render the mail in "text/html" or
    20  // "text/plain".
    21  func devMailsHandler(c echo.Context) error {
    22  	name := c.Param("name")
    23  	locale := c.QueryParam("locale")
    24  	if locale == "" {
    25  		locale = statik.GetLanguageFromHeader(c.Request().Header)
    26  	}
    27  
    28  	recipientName := c.QueryParam("RecipientName")
    29  	if recipientName == "" {
    30  		recipientName = "Jean Dupont"
    31  	}
    32  
    33  	layout := c.QueryParam("layout")
    34  	if layout == "" {
    35  		layout = "layout"
    36  	}
    37  
    38  	data := devData(c)
    39  	j := &job.Job{JobID: "1", Domain: data["Domain"].(string)}
    40  	inst := middlewares.GetInstance(c)
    41  	ctx, cancel := job.NewTaskContext("0", j, inst)
    42  	defer cancel()
    43  	_, parts, err := mails.RenderMail(ctx, name, layout, locale, recipientName, data)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	contentType := c.QueryParam("ContentType")
    49  	if contentType == "" {
    50  		contentType = "text/html"
    51  	}
    52  
    53  	var part *mail.Part
    54  	for _, p := range parts {
    55  		if p.Type == contentType {
    56  			part = p
    57  		}
    58  	}
    59  	if part == nil {
    60  		return echo.NewHTTPError(http.StatusNotFound,
    61  			fmt.Errorf("Could not find template %q with content-type %q", name, contentType))
    62  	}
    63  
    64  	// Remove all CSP policies to display HTML email. this is a dev-only
    65  	// handler, no need to worry.
    66  	c.Response().Header().Set(echo.HeaderContentSecurityPolicy, "")
    67  	if part.Type == "text/html" {
    68  		return c.HTML(http.StatusOK, part.Body)
    69  	}
    70  	return c.String(http.StatusOK, part.Body)
    71  }
    72  
    73  // devTemplatesHandler allow to easily render a given template from a route of
    74  // the stack. The query parameters are used as data input for the template.
    75  func devTemplatesHandler(c echo.Context) error {
    76  	name := c.Param("name")
    77  	return c.Render(http.StatusOK, name, devData(c))
    78  }
    79  
    80  func devData(c echo.Context) echo.Map {
    81  	data := make(echo.Map)
    82  	data["Domain"] = c.Request().Host
    83  	data["ContextName"] = config.DefaultInstanceContext
    84  	data["Illustration"] = "/images/generic-error.svg"
    85  	if i, err := lifecycle.GetInstance(c.Request().Host); err == nil {
    86  		data["Domain"] = i.ContextualDomain()
    87  		data["ContextName"] = i.ContextName
    88  		data["Locale"] = i.Locale
    89  		data["Title"] = i.TemplateTitle()
    90  		data["Favicon"] = middlewares.Favicon(i)
    91  		data["InstanceURL"] = i.PageURL("/", nil)
    92  		data["SupportEmail"] = i.SupportEmailAddress()
    93  	}
    94  	for k, v := range c.QueryParams() {
    95  		if len(v) > 0 {
    96  			data[k] = v[0]
    97  		}
    98  	}
    99  	return data
   100  }