From 1bd50201800a6c909541eb2bb6249d21a8c958c3 Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Thu, 5 Nov 2020 00:24:29 +0100 Subject: [PATCH] added visualisation --- climate-server.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/climate-server.go b/climate-server.go index 3298166..5631f80 100644 --- a/climate-server.go +++ b/climate-server.go @@ -1,11 +1,13 @@ package main import ( + "errors" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/gorilla/mux" "log" "net/http" + "strconv" ) const DEBUG = true @@ -34,7 +36,7 @@ func startServer() { port := "8001" r := mux.NewRouter() r.HandleFunc("/", showCharts).Methods("GET") - r.HandleFunc("/data/", sendData).Methods("GET") + r.HandleFunc("/data/{count}", sendData).Methods("GET") r.HandleFunc("/", saveSnapshot).Methods("POST") r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) http.Handle("/", r) @@ -47,7 +49,17 @@ func showCharts(w http.ResponseWriter, r *http.Request) { } func sendData(w http.ResponseWriter, r *http.Request) { - records, err := getSnapshotRecordsFromDb(50) + vars := mux.Vars(r) + var count int64 = 50 + if vars["count"] != "" { + newCount, err := strconv.ParseInt(vars["count"], 10, 0) + if err != nil { + sendInternalError(errors.New("bad snapshot count request"), w, r) + return + } + count = newCount + } + records, err := getSnapshotRecordsFromDb(int(count)) if err != nil { sendInternalError(fmt.Errorf("couldn't read rows from the database: %w", err), w, r) return