2023-12-18 10:46:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-18 14:05:18 +00:00
|
|
|
"bytes"
|
2023-12-18 10:46:41 +00:00
|
|
|
"embed"
|
2023-12-18 14:05:18 +00:00
|
|
|
"encoding/csv"
|
2023-12-18 14:38:44 +00:00
|
|
|
"flag"
|
2023-12-18 12:32:51 +00:00
|
|
|
"fmt"
|
2023-12-18 10:46:41 +00:00
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-12-18 12:32:51 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-12-18 10:46:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type EmojiDetails struct {
|
|
|
|
Label string
|
2023-12-18 11:56:51 +00:00
|
|
|
Url string
|
2023-12-18 10:46:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 14:38:44 +00:00
|
|
|
type Config struct {
|
|
|
|
RequestsFile string
|
|
|
|
Listen string
|
|
|
|
}
|
|
|
|
|
2023-12-18 10:46:41 +00:00
|
|
|
//go:embed templates
|
|
|
|
var indexHTML embed.FS
|
2023-12-18 14:38:44 +00:00
|
|
|
var config Config
|
2023-12-18 10:46:41 +00:00
|
|
|
|
2023-12-18 10:54:21 +00:00
|
|
|
func handleForm(w http.ResponseWriter, r *http.Request) {
|
2023-12-18 10:46:41 +00:00
|
|
|
tmpl := template.Must(template.ParseFS(indexHTML, "templates/index.html"))
|
|
|
|
|
2023-12-18 10:54:21 +00:00
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
tmpl.Execute(w, nil)
|
|
|
|
return
|
|
|
|
}
|
2023-12-18 10:46:41 +00:00
|
|
|
|
2023-12-18 10:54:21 +00:00
|
|
|
details := EmojiDetails{
|
|
|
|
Label: r.FormValue("label"),
|
2023-12-18 11:56:51 +00:00
|
|
|
Url: r.FormValue("url"),
|
2023-12-18 10:54:21 +00:00
|
|
|
}
|
2023-12-18 10:46:41 +00:00
|
|
|
|
2023-12-18 12:32:51 +00:00
|
|
|
now := time.Now().UTC().Format(time.RFC3339)
|
2023-12-18 14:05:18 +00:00
|
|
|
entry := strings.Join([]string{now, "unknown", details.Label, details.Url}, ",")
|
2023-12-18 12:32:51 +00:00
|
|
|
insertEntry(entry)
|
2023-12-18 10:46:41 +00:00
|
|
|
|
2023-12-18 10:54:21 +00:00
|
|
|
tmpl.Execute(w, struct{ Success bool }{true})
|
|
|
|
}
|
2023-12-18 10:46:41 +00:00
|
|
|
|
2023-12-18 14:05:18 +00:00
|
|
|
func handleRequested(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tmpl := template.Must(template.ParseFS(indexHTML, "templates/requested.html"))
|
2023-12-18 14:38:44 +00:00
|
|
|
file, _ := os.ReadFile(config.RequestsFile)
|
2023-12-18 14:05:18 +00:00
|
|
|
requests, _ := csv.NewReader(bytes.NewReader(file)).ReadAll()
|
|
|
|
|
|
|
|
tmpl.Execute(w, struct{ Requests [][]string }{requests})
|
|
|
|
}
|
|
|
|
|
2023-12-18 10:54:21 +00:00
|
|
|
func setupRoutes() {
|
|
|
|
http.HandleFunc("/", handleForm)
|
2023-12-18 14:05:18 +00:00
|
|
|
http.HandleFunc("/requested", handleRequested)
|
2023-12-18 14:38:44 +00:00
|
|
|
err := http.ListenAndServe(config.Listen, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-12-18 10:46:41 +00:00
|
|
|
}
|
2023-12-18 10:54:21 +00:00
|
|
|
|
2023-12-18 12:32:51 +00:00
|
|
|
func insertEntry(entry string) {
|
2023-12-18 14:38:44 +00:00
|
|
|
f, err := os.OpenFile(config.RequestsFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
2023-12-18 12:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
entryFormatted := fmt.Sprintf("%s\n", entry)
|
|
|
|
if _, err := f.WriteString(entryFormatted); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 10:54:21 +00:00
|
|
|
func main() {
|
2023-12-18 14:38:44 +00:00
|
|
|
requestsFile := flag.String("requests", "./requests.txt", "Where the file containing all the requests is")
|
|
|
|
listen := flag.String("listen", ":8080", "The listen address and port")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
config.RequestsFile = *requestsFile
|
|
|
|
config.Listen = *listen
|
|
|
|
|
2023-12-18 10:54:21 +00:00
|
|
|
setupRoutes()
|
|
|
|
}
|