add requested emojis page

This commit is contained in:
fungal 2023-12-18 15:05:18 +01:00
parent 3ff710206f
commit 9da55bcde9
3 changed files with 87 additions and 4 deletions

14
main.go
View file

@ -1,7 +1,9 @@
package main
import (
"bytes"
"embed"
"encoding/csv"
"fmt"
"html/template"
"log"
@ -33,15 +35,23 @@ func handleForm(w http.ResponseWriter, r *http.Request) {
}
now := time.Now().UTC().Format(time.RFC3339)
entry := strings.Join([]string{now, details.Label, details.Url}, ",")
log.Print("inserting: ", entry)
entry := strings.Join([]string{now, "unknown", details.Label, details.Url}, ",")
insertEntry(entry)
tmpl.Execute(w, struct{ Success bool }{true})
}
func handleRequested(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFS(indexHTML, "templates/requested.html"))
file, _ := os.ReadFile("requests.txt")
requests, _ := csv.NewReader(bytes.NewReader(file)).ReadAll()
tmpl.Execute(w, struct{ Requests [][]string }{requests})
}
func setupRoutes() {
http.HandleFunc("/", handleForm)
http.HandleFunc("/requested", handleRequested)
http.ListenAndServe(":8080", nil)
}

View file

@ -10,14 +10,22 @@
padding: 2rem;
margin: auto;
}
nav>a {
margin: 1ch;
}
</style>
</head>
<body>
<nav>
<a href="/">home</a>
<a href="/requested">requested</a>
</nav>
<main>
{{if .Success}}
{{- if .Success }}
<h1>emoji requested successfully</h1>
{{end}}
{{- end }}
<h1>Request An Emoji</h1>
<form method="POST">
<div>

65
templates/requested.html Normal file
View file

@ -0,0 +1,65 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Emoji Request</title>
<style>
main {
max-width: 70ch;
padding: 2rem;
margin: auto;
}
nav>a {
margin: 1ch;
}
table {
display: block;
overflow-x: auto;
white-space: nowrap;
border-spacing: 2ch 1ch;
}
table tbody {
width: 100%;
}
th {
text-align: start;
}
</style>
</head>
<body>
<nav>
<a href="/">home</a>
<a href="/requested">requested</a>
</nav>
<main>
<h1>Requested Emojis</h1>
<table>
<thead>
<tr>
<th>At</th>
<th>By</th>
<th>Shortcode</th>
<th>URL</th>
</tr>
</thead>
<tbody>
{{- range $row := .Requests }}
<tr>
<td>{{index $row 0 }}</td>
<td>{{index $row 1 }}</td>
<td>{{index $row 2 }}</td>
<td>{{index $row 3 }}</td>
</tr>
{{- end }}
</tbody>
</table>
</main>
</body>
</html>