diff options
author | Aki <please@ignore.pl> | 2020-07-08 00:57:18 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2020-07-08 00:57:18 +0200 |
commit | 61fa862c8e7054f13373317fc9a6a78cbb37e313 (patch) | |
tree | d63750c1ccdbcf0594bcb4da1d36d1fdf94ca89a /main.go | |
download | rudone-61fa862c8e7054f13373317fc9a6a78cbb37e313.zip rudone-61fa862c8e7054f13373317fc9a6a78cbb37e313.tar.gz rudone-61fa862c8e7054f13373317fc9a6a78cbb37e313.tar.bz2 |
Initial backend that collects entries
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -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() +} |