Preview command for serving preview of order confirmation template

This commit is contained in:
2022-12-02 19:01:07 +01:00
parent c6157ef5b7
commit 14f7f55b4f
5 changed files with 71 additions and 2 deletions

View File

@@ -20,6 +20,11 @@ server:
export GO111MODULE="on"; \
go run -race cmd/server/main.go
## preview: runs preview of mail template
preview:
export GO111MODULE="on"; \
go run cmd/preview/main.go
## modd: Monitors the directory, and recompiles your app every time a file changes. Also runs tests.
## (To install modd, run: go get github.com/cortesi/modd/cmd/modd)
modd:

View File

@@ -26,4 +26,10 @@ Run as docker container using:
or
```bash
(sudo) docker run -d --name planetposen-mail -p 8000:8000 --env-file .env planetposen-mail
```
## Preview
Use view template as preview during local development run:
```bash
make preview
```

57
cmd/preview/main.go Normal file
View File

@@ -0,0 +1,57 @@
package main
import (
"bufio"
"bytes"
"fmt"
"html/template"
"log"
"net/http"
)
type Product struct {
Name string
Image string
Description string
Quantity int
Price float32
Currency string
}
type ReceiptPageData struct {
PageTitle string
OrderId string
Products []Product
}
func serveTemplate(response http.ResponseWriter, request *http.Request) {
tmpl := template.Must(template.ParseFiles("mail/mail-template_order-confirmation.html"))
// tmpl := template.Must(w)
data := ReceiptPageData{
PageTitle: "Planetposen purchase",
OrderId: "fb9a5910-0dcf-4c65-9c25-3fb3eb883ce5",
Products: []Product{
{Name: "Forrest", Image: "https://planet.schleppe.cloud/email/items/item-1.jpg", Description: "Sneaker Maker", Quantity: 4, Price: 49.99, Currency: "NOK"},
{Name: "Cookie-Man Forrest", Image: "https://planet.schleppe.cloud/email/items/item-2.jpg", Description: "Boots Brothers", Quantity: 3, Price: 99, Currency: "NOK"},
{Name: "Floral", Image: "https://planet.schleppe.cloud/email/items/item-3.jpg", Description: "Swiss Made", Quantity: 1, Price: 129, Currency: "NOK"},
},
}
var b bytes.Buffer
template := bufio.NewWriter(&b)
err := tmpl.Execute(template, data)
if err != nil {
fmt.Println(err)
}
response.Header().Set("Content-Type", "text/html")
response.WriteHeader(http.StatusOK)
response.Write([]byte(b.String()))
}
func main() {
ADDRESS := ":5006"
fmt.Printf("Serving preview of template at %s\n", ADDRESS)
http.HandleFunc("/", serveTemplate)
log.Fatal(http.ListenAndServe(ADDRESS, nil))
}

2
go.mod
View File

@@ -10,4 +10,4 @@ require (
github.com/sirupsen/logrus v1.9.0
)
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
require golang.org/x/sys v0.2.0 // indirect

3
go.sum
View File

@@ -17,8 +17,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=