From 14f7f55b4f0523bbc6ba22ec6cffe2255a454ce7 Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Fri, 2 Dec 2022 19:01:07 +0100 Subject: [PATCH] Preview command for serving preview of order confirmation template --- Makefile | 5 ++++ README.md | 6 +++++ cmd/preview/main.go | 57 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 3 ++- 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 cmd/preview/main.go diff --git a/Makefile b/Makefile index 191720c..13b9047 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/README.md b/README.md index e835855..3f6ae17 100644 --- a/README.md +++ b/README.md @@ -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 ``` \ No newline at end of file diff --git a/cmd/preview/main.go b/cmd/preview/main.go new file mode 100644 index 0000000..7e2c88f --- /dev/null +++ b/cmd/preview/main.go @@ -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)) +} diff --git a/go.mod b/go.mod index 5fa38c3..85dbd59 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 456d37f..6e1880a 100644 --- a/go.sum +++ b/go.sum @@ -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=