dashboard: use last path item to handle TP number selection

This commit is contained in:
nemunaire 2020-02-28 13:23:11 +01:00
commit 8d55ecc3af
3 changed files with 41 additions and 16 deletions

View file

@ -7,6 +7,7 @@ import (
"net/http"
"path"
"strings"
"strconv"
"github.com/julienschmidt/httprouter"
)
@ -61,20 +62,34 @@ func init() {
w.Write(data)
}
})
Router().GET("/dashboard/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/css")
if data, err := Asset(path.Join("htdocs", strings.TrimPrefix(r.URL.Path, "/dashboard"))); err != nil {
Router().GET("/dashboard/:where", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if _, err := strconv.ParseInt(string(ps.ByName("where")), 10, 64); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
if data, err := Asset("htdocs/dashboard.html"); err != nil {
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
}
})
Router().GET("/dashboard/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/javascript")
if data, err := Asset(path.Join("htdocs", strings.TrimPrefix(r.URL.Path, "/dashboard"))); err != nil {
http.NotFound(w, r)
Router().GET("/dashboard/:where/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if ps.ByName("where") == "css" {
w.Header().Set("Content-Type", "text/css")
if data, err := Asset(path.Join("htdocs", strings.TrimPrefix(r.URL.Path, "/dashboard"))); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
}
} else if ps.ByName("where") == "js" {
w.Header().Set("Content-Type", "text/javascript")
if data, err := Asset(path.Join("htdocs", strings.TrimPrefix(r.URL.Path, "/dashboard"))); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
}
} else {
w.Write(data)
http.NotFound(w, r)
}
})