finding a bug

This commit is contained in:
Daniel Ledda
2020-11-01 01:44:31 +01:00
parent 746e1010fa
commit 45b3cea9a1

View File

@@ -47,15 +47,24 @@ func main() {
} }
func viewHandler(w http.ResponseWriter, r *http.Request) { func viewHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method) switch r.Method {
var snapshotSub SnapshotSubmission case "GET":
body, err := ioutil.ReadAll(r.Body) _, _ = fmt.Fprint(w, "<h1>Climate Stuff</h1><div>The data will show up here at some stage...</div>")
if err != nil { case "POST":
log.Fatal(err) var snapshotSub SnapshotSubmission
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(body, &snapshotSub)
if err != nil {
log.Fatal(err)
}
_, _ = climateDb.Query(
"INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, %v, NULL);",
snapshotSub.Temp,
snapshotSub.Humidity,
snapshotSub.Timestamp,
snapshotSub.Co2)
} }
err = json.Unmarshal(body, &snapshotSub)
if err != nil {
log.Fatal(err)
}
_, _ = fmt.Fprint(w, "<h1>Climate Stuff</h1><div>The data will show up here at some stage...</div>")
} }