Almost there

This commit is contained in:
Daniel Ledda
2020-11-14 15:19:50 +01:00
parent 140fdfe418
commit 9f3301f965

View File

@@ -13,11 +13,11 @@ import (
"time"
)
const ROOT_URL = "/climate/"
const ROOT_URL = "/climate"
func startServer() {
port := "8001"
router := mux.NewRouter().StrictSlash(true)
router := mux.NewRouter()
router.HandleFunc("/show", showCharts).Methods("GET")
router.HandleFunc("/show", showCharts).Methods("GET").Queries("show-minutes", "{[0-9]+}")
router.HandleFunc("/data", sendData).Methods("GET")
@@ -33,12 +33,12 @@ func showCharts(w http.ResponseWriter, r *http.Request) {
minutes := r.FormValue("show-minutes")
if minutes != "" {
if _, err := strconv.ParseInt(minutes, 10, 0); err != nil {
http.Redirect(w, r, ROOT_URL, 303)
http.Redirect(w, r, ROOT_URL + "/show", 303)
}
}
templater, err := template.ParseFiles("webapp/dist/charts.html")
if internalErrorOnErr(fmt.Errorf("couldn't parse the template: %w", err), w, r) { return }
err = templater.Execute(w, ROOT_URL)
err = templater.Execute(w, ROOT_URL + "/show")
internalErrorOnErr(err, w, r)
}
@@ -48,7 +48,7 @@ func sendData(w http.ResponseWriter, r *http.Request) {
if sinceQuery != "" {
newDateSince, err := time.Parse(time.RFC3339Nano, sinceQuery)
if err != nil {
http.Redirect(w, r, ROOT_URL, 303)
http.Redirect(w, r, ROOT_URL + "/show", 303)
return
}
dateSince = strings.Split(newDateSince.Format(time.RFC3339Nano), "Z")[0]