save request to local file, csv format

This commit is contained in:
fungal 2023-12-18 13:32:51 +01:00
parent 3c029c9563
commit 441927946c

23
main.go
View file

@ -2,9 +2,13 @@ package main
import ( import (
"embed" "embed"
"fmt"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"os"
"strings"
"time"
) )
type EmojiDetails struct { type EmojiDetails struct {
@ -28,8 +32,10 @@ func handleForm(w http.ResponseWriter, r *http.Request) {
Url: r.FormValue("url"), Url: r.FormValue("url"),
} }
log.Print(details.Label) now := time.Now().UTC().Format(time.RFC3339)
log.Print(details.Url) entry := strings.Join([]string{now, details.Label, details.Url}, ",")
log.Print("inserting: ", entry)
insertEntry(entry)
tmpl.Execute(w, struct{ Success bool }{true}) tmpl.Execute(w, struct{ Success bool }{true})
} }
@ -39,6 +45,19 @@ func setupRoutes() {
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }
func insertEntry(entry string) {
f, err := os.OpenFile("requests.txt",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
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)
}
}
func main() { func main() {
setupRoutes() setupRoutes()
} }