From 8d55ecc3af686cfb7a91cb6856e4b7fcb06c2fa3 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 28 Feb 2020 13:23:11 +0100 Subject: [PATCH] dashboard: use last path item to handle TP number selection --- token-validator/htdocs/js/adlin-dashboard.js | 8 ++++- token-validator/static-dev.go | 12 ++++--- token-validator/static.go | 33 ++++++++++++++------ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/token-validator/htdocs/js/adlin-dashboard.js b/token-validator/htdocs/js/adlin-dashboard.js index 10402f4..55fa646 100644 --- a/token-validator/htdocs/js/adlin-dashboard.js +++ b/token-validator/htdocs/js/adlin-dashboard.js @@ -38,7 +38,13 @@ angular.module("AdLinApp", ["ngResource", "ngSanitize"]) angular.module("AdLinApp") .run(function($rootScope, $location) { - $rootScope.tutoid = 0; + if (window.location.pathname.split("/").length >= 3) { + $rootScope.tutoid = parseInt(window.location.pathname.split("/")[2], 10); + } + if (!$rootScope.tutoid || isNaN($rootScope.tutoid)) { + $rootScope.tutoid = 1; + } + $rootScope.tutoid--; }) .controller("StudentsController", function($scope, $interval, Student) { $scope.students = Student.query(); diff --git a/token-validator/static-dev.go b/token-validator/static-dev.go index d95ce3c..c35bc5d 100644 --- a/token-validator/static-dev.go +++ b/token-validator/static-dev.go @@ -8,6 +8,7 @@ import ( "os" "path" "path/filepath" + "strconv" "strings" "github.com/julienschmidt/httprouter" @@ -38,11 +39,14 @@ func init() { Router().GET("/dashboard/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.ServeFile(w, r, path.Join(StaticDir, "dashboard.html")) }) - Router().GET("/dashboard/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - r.URL.Path = strings.TrimPrefix(r.URL.Path, "/dashboard") - serveStaticAsset(w, r) + 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 { + http.ServeFile(w, r, path.Join(StaticDir, "dashboard.html")) + } }) - Router().GET("/dashboard/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + Router().GET("/dashboard/:where/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { r.URL.Path = strings.TrimPrefix(r.URL.Path, "/dashboard") serveStaticAsset(w, r) }) diff --git a/token-validator/static.go b/token-validator/static.go index 63f44a2..d7f2603 100644 --- a/token-validator/static.go +++ b/token-validator/static.go @@ -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) } })