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 package main
import ( import (
"bytes"
"embed" "embed"
"encoding/csv"
"fmt" "fmt"
"html/template" "html/template"
"log" "log"
@ -33,15 +35,23 @@ func handleForm(w http.ResponseWriter, r *http.Request) {
} }
now := time.Now().UTC().Format(time.RFC3339) now := time.Now().UTC().Format(time.RFC3339)
entry := strings.Join([]string{now, details.Label, details.Url}, ",") entry := strings.Join([]string{now, "unknown", details.Label, details.Url}, ",")
log.Print("inserting: ", entry)
insertEntry(entry) insertEntry(entry)
tmpl.Execute(w, struct{ Success bool }{true}) 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() { func setupRoutes() {
http.HandleFunc("/", handleForm) http.HandleFunc("/", handleForm)
http.HandleFunc("/requested", handleRequested)
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }

View file

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