finding a bug

This commit is contained in:
Daniel Ledda
2020-11-01 01:06:50 +01:00
parent 38e2211df7
commit 7ada19a40a

View File

@@ -1,9 +1,11 @@
package main package main
import ( import (
"encoding/json"
"database/sql" "database/sql"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@@ -11,7 +13,7 @@ import (
"time" "time"
) )
type Snapshot struct { type SnapshotRecord struct {
Id int Id int
Timestamp time.Time Timestamp time.Time
Temp float32 Temp float32
@@ -19,6 +21,14 @@ type Snapshot struct {
Co2 float32 Co2 float32
} }
type SnapshotSubmission struct {
Timestamp time.Time `json:"time"`
Temp float32 `json:"temp"`
Humidity float32 `json:"humidity"`
Co2 float32 `json:"co2"`
}
var climateDb *sql.DB var climateDb *sql.DB
func setupDb() *sql.DB { func setupDb() *sql.DB {
@@ -39,11 +49,15 @@ func main() {
func viewHandler(w http.ResponseWriter, r *http.Request) { func viewHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Request received") fmt.Println("Request received")
reqBody, err := ioutil.ReadAll(r.Body) var snapshotSub SnapshotSubmission
body, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Printf("%s\n", reqBody) err = json.Unmarshal(body, &snapshotSub)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", snapshotSub)
_, _ = fmt.Fprint(w, "<h1>Climate Stuff</h1><div>The data will show up here at some stage...</div>") _, _ = fmt.Fprint(w, "<h1>Climate Stuff</h1><div>The data will show up here at some stage...</div>")
} }