render inbox as html instead of json

This commit is contained in:
Guilherme Rugai Freire 2024-02-20 18:02:30 -03:00
parent 95252f12f5
commit f4407d71f8
No known key found for this signature in database
GPG Key ID: AC1D9B6E48E16AC1
2 changed files with 33 additions and 11 deletions

View File

@ -0,0 +1,30 @@
package main
import "time"
templ inbox_body(rcpt_addr string, ms []mail) {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>nthmail.xyz</title>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta name="description" content="A temporary mail service"/>
</head>
<body>
<h1>Emails for { rcpt_addr }:</h1>
<ul>
for _, m := range ms {
<li>
@mail_comp(m)
</li>
}
</ul>
</body>
</html>
}
templ mail_comp(m mail) {
<h3>From: &lt{ m.From_addr }&gt at { time.Unix(m.Arrived_at, 0).Format(time.DateTime) }</h3>
<p>{ string(m.Data) }</p>
}

View File

@ -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