From 61fa862c8e7054f13373317fc9a6a78cbb37e313 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 8 Jul 2020 00:57:18 +0200 Subject: Initial backend that collects entries --- main.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 main.go (limited to 'main.go') diff --git a/main.go b/main.go new file mode 100644 index 0000000..1b5dd5b --- /dev/null +++ b/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "encoding/json" + "github.com/gorilla/mux" + "log" + "net/http" +) + +func handleEntry(w http.ResponseWriter, r *http.Request) { + var entry Entry + + err := json.NewDecoder(r.Body).Decode(&entry) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + err = InsertEntry(&entry) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +func handleRequests() { + router := mux.NewRouter() + router.HandleFunc("/entry", handleEntry).Methods("POST") + + log.Fatal(http.ListenAndServe(":8080", router)) +} + +func main() { + log.Println("Starting up") + InitEntries() + defer CloseEntries() + handleRequests() +} -- cgit v1.1