mirror of
https://codeberg.org/mycelium/emojiquest.git
synced 2024-11-17 06:33:17 +00:00
save request to local file, csv format
This commit is contained in:
parent
3c029c9563
commit
441927946c
23
main.go
23
main.go
|
@ -2,9 +2,13 @@ package main
|
|||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type EmojiDetails struct {
|
||||
|
@ -28,8 +32,10 @@ func handleForm(w http.ResponseWriter, r *http.Request) {
|
|||
Url: r.FormValue("url"),
|
||||
}
|
||||
|
||||
log.Print(details.Label)
|
||||
log.Print(details.Url)
|
||||
now := time.Now().UTC().Format(time.RFC3339)
|
||||
entry := strings.Join([]string{now, details.Label, details.Url}, ",")
|
||||
log.Print("inserting: ", entry)
|
||||
insertEntry(entry)
|
||||
|
||||
tmpl.Execute(w, struct{ Success bool }{true})
|
||||
}
|
||||
|
@ -39,6 +45,19 @@ func setupRoutes() {
|
|||
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() {
|
||||
setupRoutes()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue