Changed endpoints and db queries, chart now shows suggested mins and maxes

This commit is contained in:
Daniel Ledda
2020-11-07 00:50:36 +01:00
parent 69ee100edd
commit 40b23ebcde
3 changed files with 35 additions and 28 deletions

View File

@@ -36,8 +36,9 @@ func startServer() {
port := "8001"
r := mux.NewRouter()
r.HandleFunc("/", showCharts).Methods("GET")
r.HandleFunc("/since/{mins}", showCharts).Methods("GET")
r.HandleFunc("/data/", sendData).Methods("GET")
r.HandleFunc("/data/{count}", sendData).Methods("GET")
r.HandleFunc("/data/since/{mins}", sendData).Methods("GET")
r.HandleFunc("/", saveSnapshot).Methods("POST")
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
http.Handle("/", r)
@@ -46,13 +47,19 @@ func startServer() {
}
func showCharts(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "charts.html")
if countStr := mux.Vars(r)["mins"]; countStr != "" {
if _, err := strconv.ParseInt(countStr, 10, 0); err != nil {
http.Redirect(w, r, "/", 303)
}
} else {
http.ServeFile(w, r, "charts.html")
}
}
func sendData(w http.ResponseWriter, r *http.Request) {
var count int64 = 180
if vars := mux.Vars(r); vars["count"] != "" {
newCount, err := strconv.ParseInt(vars["count"], 10, 0)
var count int64 = 60
if vars := mux.Vars(r); vars["mins"] != "" {
newCount, err := strconv.ParseInt(vars["mins"], 10, 0)
if err != nil {
sendInternalError(errors.New("bad snapshot count request"), w, r)
return