From f4407d71f807c4744638ef8ce68d292903c4c85c Mon Sep 17 00:00:00 2001 From: Guilherme Rugai Freire Date: Tue, 20 Feb 2024 18:02:30 -0300 Subject: [PATCH] render inbox as html instead of json --- cmd/web_server/inbox.templ | 30 ++++++++++++++++++++++++++++++ cmd/web_server/main.go | 14 +++----------- 2 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 cmd/web_server/inbox.templ diff --git a/cmd/web_server/inbox.templ b/cmd/web_server/inbox.templ new file mode 100644 index 0000000..9f0b7e2 --- /dev/null +++ b/cmd/web_server/inbox.templ @@ -0,0 +1,30 @@ +package main + +import "time" + +templ inbox_body(rcpt_addr string, ms []mail) { + + + + + nthmail.xyz + + + + +

Emails for { rcpt_addr }:

+ + + +} + +templ mail_comp(m mail) { +

From: <{ m.From_addr }> at { time.Unix(m.Arrived_at, 0).Format(time.DateTime) }

+

{ string(m.Data) }

+} diff --git a/cmd/web_server/main.go b/cmd/web_server/main.go index 8b0e97c..81aebe1 100644 --- a/cmd/web_server/main.go +++ b/cmd/web_server/main.go @@ -2,7 +2,6 @@ package main import ( "database/sql" - "encoding/json" "fmt" "log" "net/http" @@ -16,7 +15,7 @@ import ( type mail struct { Id int - Arrived_at int + Arrived_at int64 Rcpt_addr, From_addr string Data []byte } @@ -98,16 +97,9 @@ func main() { mails = append(mails, m) } - b, err := json.Marshal(mails) - if err != nil { - res.WriteHeader(500) - res.Write([]byte("internal server error")) - log.Println("could not marshal json") - return - } - - res.Write([]byte(b)) + body := inbox_body(rcpt_addr, mails) + body.Render(req.Context(), res) }) var port int