add templ and initial frontend code

This commit is contained in:
Guilherme Rugai Freire 2024-02-20 16:13:44 -03:00
parent e07aeea312
commit 40900f3112
No known key found for this signature in database
GPG Key ID: AC1D9B6E48E16AC1
7 changed files with 50 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
bin/*
!bin/.gitkeep
*_templ.go
*.db

0
bin/.gitkeep Normal file
View File

View File

@ -0,0 +1,5 @@
package main
templ index(appname string) {
<h1>Home for { appname }</h1>
}

View File

@ -29,6 +29,11 @@ func main() {
router := chi.NewRouter()
router.Get("/", func(res http.ResponseWriter, req *http.Request) {
component := index("nthmail.xyz")
component.Render(req.Context(), res)
})
router.Get("/{rcpt-addr}", func(res http.ResponseWriter, req *http.Request) {
rcpt_addr := chi.URLParam(req, "rcpt-addr")
if len(rcpt_addr) == 0 {
@ -78,7 +83,7 @@ func main() {
return
}
mails = append(mails, m)
mails = append(mails, m)
}
b, err := json.Marshal(mails)
if err != nil {
@ -104,5 +109,8 @@ func main() {
}
log.Println("Listening on port", port)
http.ListenAndServe(fmt.Sprintf(":%d", port), router)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), router)
if err != nil {
log.Fatal(err)
}
}

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/GRFreire/nthmail
go 1.21.6
require (
github.com/a-h/templ v0.2.543 // indirect
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 // indirect
github.com/emersion/go-smtp v0.20.2 // indirect
github.com/go-chi/chi v1.5.5 // indirect

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/a-h/templ v0.2.543 h1:8YyLvyUtf0/IE2nIwZ62Z/m2o2NqwhnMynzOL78Lzbk=
github.com/a-h/templ v0.2.543/go.mod h1:jP908DQCwI08IrnTalhzSEH9WJqG/Q94+EODQcJGFUA=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.20.2 h1:peX42Qnh5Q0q3vrAnRy43R/JwTnnv75AebxbkTL7Ia4=

31
run-web.sh Executable file
View File

@ -0,0 +1,31 @@
#!/bin/sh
watch_dir="cmd/web_server"
pid=0
get_ts() {
stat "$watch_dir" | grep Modify | awk '{$1=""; print $0}' | sed 's/^ //g'
}
run_server() {
make -B web
./bin/web_server &
pid=$!
}
ts="$(get_ts)"
run_server
while true; do
sleep 1;
new_ts="$(get_ts)"
if [ "$ts" != "$new_ts" ]; then
ts="$new_ts"
echo ""
if [ "$pid" != "0" ]; then
kill -s KILL $pid
fi
run_server
fi
done