From d24e261db2fc9c17c3a142a38575d3d92c9f5b82 Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Tue, 1 Feb 2022 17:48:56 +0100 Subject: [PATCH] Rename templates and port ClusterCockpit navbar + layout --- auth/auth.go | 6 +- server.go | 96 +++++++++----- templates/{404.html => 404.tmpl} | 2 +- templates/base.html | 28 ----- templates/base.tmpl | 119 ++++++++++++++++++ templates/{home.html => home.tmpl} | 0 templates/login.html | 47 ------- templates/login.tmpl | 41 ++++++ .../{analysis.html => analysis.tmpl} | 0 templates/monitoring/{job.html => job.tmpl} | 0 templates/monitoring/{jobs.html => jobs.tmpl} | 0 templates/monitoring/{list.html => list.tmpl} | 0 templates/monitoring/{node.html => node.tmpl} | 0 .../monitoring/{systems.html => systems.tmpl} | 0 templates/monitoring/{user.html => user.tmpl} | 0 templates/templates.go | 20 +-- 16 files changed, 237 insertions(+), 122 deletions(-) rename templates/{404.html => 404.tmpl} (87%) delete mode 100644 templates/base.html create mode 100644 templates/base.tmpl rename templates/{home.html => home.tmpl} (100%) delete mode 100644 templates/login.html create mode 100644 templates/login.tmpl rename templates/monitoring/{analysis.html => analysis.tmpl} (100%) rename templates/monitoring/{job.html => job.tmpl} (100%) rename templates/monitoring/{jobs.html => jobs.tmpl} (100%) rename templates/monitoring/{list.html => list.tmpl} (100%) rename templates/monitoring/{node.html => node.tmpl} (100%) rename templates/monitoring/{systems.html => systems.tmpl} (100%) rename templates/monitoring/{user.html => user.tmpl} (100%) diff --git a/auth/auth.go b/auth/auth.go index 30f2a94..b555683 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -190,7 +190,7 @@ func Login(db *sqlx.DB) http.Handler { if err != nil { log.Warnf("login of user %#v failed: %s", username, err.Error()) rw.WriteHeader(http.StatusUnauthorized) - templates.Render(rw, r, "login.html", &templates.Page{ + templates.Render(rw, r, "login.tmpl", &templates.Page{ Title: "Login failed", Login: &templates.LoginPage{ Error: "Username or password incorrect", @@ -291,7 +291,7 @@ func Auth(next http.Handler) http.Handler { log.Warn("authentication failed: no session or jwt found") rw.WriteHeader(http.StatusUnauthorized) - templates.Render(rw, r, "login.html", &templates.Page{ + templates.Render(rw, r, "login.tmpl", &templates.Page{ Title: "Authentication failed", Login: &templates.LoginPage{ Error: "No valid session or JWT provided", @@ -349,7 +349,7 @@ func Logout(rw http.ResponseWriter, r *http.Request) { } } - templates.Render(rw, r, "login.html", &templates.Page{ + templates.Render(rw, r, "login.tmpl", &templates.Page{ Title: "Logout successful", Login: &templates.LoginPage{ Info: "Logout successful", diff --git a/server.go b/server.go index 6861b07..8dad8dc 100644 --- a/server.go +++ b/server.go @@ -268,7 +268,7 @@ func main() { } handleGetLogin := func(rw http.ResponseWriter, r *http.Request) { - templates.Render(rw, r, "login.html", &templates.Page{ + templates.Render(rw, r, "login.tmpl", &templates.Page{ Title: "Login", Login: &templates.LoginPage{}, }) @@ -276,7 +276,7 @@ func main() { r := mux.NewRouter() r.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - templates.Render(rw, r, "404.html", &templates.Page{ + templates.Render(rw, r, "404.tmpl", &templates.Page{ Title: "Not found", }) }) @@ -301,16 +301,17 @@ func main() { infos := map[string]interface{}{ "clusters": config.Clusters, - "username": "", - "admin": true, } if user := auth.GetUser(r.Context()); user != nil { infos["username"] = user.Username infos["admin"] = user.HasRole(auth.RoleAdmin) + } else { + infos["username"] = false + infos["admin"] = false } - templates.Render(rw, r, "home.html", &templates.Page{ + templates.Render(rw, r, "home.tmpl", &templates.Page{ Title: "ClusterCockpit", Config: conf, Infos: infos, @@ -393,6 +394,27 @@ func main() { log.Print("Gracefull shutdown completed!") } +func prepareRoute(r *http.Request) (map[string]interface{}, map[string]interface{}, error) { + conf, err := config.GetUIConfig(r) + if err != nil { + return nil, nil, err + } + + infos := map[string]interface{}{ + "admin": true, + } + + if user := auth.GetUser(r.Context()); user != nil { + infos["username"] = user.Username + infos["admin"] = user.HasRole(auth.RoleAdmin) + } else { + infos["username"] = false + infos["admin"] = false + } + + return conf, infos, nil +} + func monitoringRoutes(router *mux.Router, resolver *graph.Resolver) { buildFilterPresets := func(query url.Values) map[string]interface{} { filterPresets := map[string]interface{}{} @@ -444,21 +466,22 @@ func monitoringRoutes(router *mux.Router, resolver *graph.Resolver) { } router.HandleFunc("/monitoring/jobs/", func(rw http.ResponseWriter, r *http.Request) { - conf, err := config.GetUIConfig(r) + conf, infos, err := prepareRoute(r) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return } - templates.Render(rw, r, "monitoring/jobs.html", &templates.Page{ + templates.Render(rw, r, "monitoring/jobs.tmpl", &templates.Page{ Title: "Jobs - ClusterCockpit", Config: conf, + Infos: infos, FilterPresets: buildFilterPresets(r.URL.Query()), }) }) router.HandleFunc("/monitoring/job/{id:[0-9]+}", func(rw http.ResponseWriter, r *http.Request) { - conf, err := config.GetUIConfig(r) + conf, infos, err := prepareRoute(r) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return @@ -471,49 +494,53 @@ func monitoringRoutes(router *mux.Router, resolver *graph.Resolver) { return } - templates.Render(rw, r, "monitoring/job.html", &templates.Page{ + infos["id"] = id + infos["jobId"] = job.JobID + infos["clusterId"] = job.Cluster + + templates.Render(rw, r, "monitoring/job.tmpl", &templates.Page{ Title: fmt.Sprintf("Job %d - ClusterCockpit", job.JobID), Config: conf, - Infos: map[string]interface{}{ - "id": id, - "jobId": job.JobID, - "clusterId": job.Cluster, - }, + Infos: infos, }) }) router.HandleFunc("/monitoring/users/", func(rw http.ResponseWriter, r *http.Request) { - conf, err := config.GetUIConfig(r) + conf, infos, err := prepareRoute(r) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return } - templates.Render(rw, r, "monitoring/list.html", &templates.Page{ + infos["listType"] = "USER" + + templates.Render(rw, r, "monitoring/list.tmpl", &templates.Page{ Title: "Users - ClusterCockpit", Config: conf, FilterPresets: buildFilterPresets(r.URL.Query()), - Infos: map[string]interface{}{"listType": "USER"}, + Infos: infos, }) }) router.HandleFunc("/monitoring/projects/", func(rw http.ResponseWriter, r *http.Request) { - conf, err := config.GetUIConfig(r) + conf, infos, err := prepareRoute(r) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return } - templates.Render(rw, r, "monitoring/list.html", &templates.Page{ + infos["listType"] = "PROJECT" + + templates.Render(rw, r, "monitoring/list.tmpl", &templates.Page{ Title: "Projects - ClusterCockpit", Config: conf, FilterPresets: buildFilterPresets(r.URL.Query()), - Infos: map[string]interface{}{"listType": "PROJECT"}, + Infos: infos, }) }) router.HandleFunc("/monitoring/user/{id}", func(rw http.ResponseWriter, r *http.Request) { - conf, err := config.GetUIConfig(r) + conf, infos, err := prepareRoute(r) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return @@ -522,17 +549,18 @@ func monitoringRoutes(router *mux.Router, resolver *graph.Resolver) { id := mux.Vars(r)["id"] // TODO: One could check if the user exists, but that would be unhelpfull if authentication // is disabled or the user does not exist but has started jobs. + infos["username"] = id - templates.Render(rw, r, "monitoring/user.html", &templates.Page{ + templates.Render(rw, r, "monitoring/user.tmpl", &templates.Page{ Title: fmt.Sprintf("User %s - ClusterCockpit", id), Config: conf, - Infos: map[string]interface{}{"username": id}, + Infos: infos, FilterPresets: buildFilterPresets(r.URL.Query()), }) }) router.HandleFunc("/monitoring/analysis/", func(rw http.ResponseWriter, r *http.Request) { - conf, err := config.GetUIConfig(r) + conf, infos, err := prepareRoute(r) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return @@ -544,15 +572,16 @@ func monitoringRoutes(router *mux.Router, resolver *graph.Resolver) { filterPresets["clusterId"] = query.Get("cluster") } - templates.Render(rw, r, "monitoring/analysis.html", &templates.Page{ + templates.Render(rw, r, "monitoring/analysis.tmpl", &templates.Page{ Title: "Analysis View - ClusterCockpit", Config: conf, + Infos: infos, FilterPresets: filterPresets, }) }) router.HandleFunc("/monitoring/systems/", func(rw http.ResponseWriter, r *http.Request) { - conf, err := config.GetUIConfig(r) + conf, infos, err := prepareRoute(r) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return @@ -564,28 +593,29 @@ func monitoringRoutes(router *mux.Router, resolver *graph.Resolver) { filterPresets["clusterId"] = query.Get("cluster") } - templates.Render(rw, r, "monitoring/systems.html", &templates.Page{ + templates.Render(rw, r, "monitoring/systems.tmpl", &templates.Page{ Title: "System View - ClusterCockpit", Config: conf, + Infos: infos, FilterPresets: filterPresets, }) }) router.HandleFunc("/monitoring/node/{clusterId}/{nodeId}", func(rw http.ResponseWriter, r *http.Request) { - conf, err := config.GetUIConfig(r) + conf, infos, err := prepareRoute(r) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return } vars := mux.Vars(r) - templates.Render(rw, r, "monitoring/node.html", &templates.Page{ + infos["nodeId"] = vars["nodeId"] + infos["clusterId"] = vars["clusterId"] + + templates.Render(rw, r, "monitoring/node.tmpl", &templates.Page{ Title: fmt.Sprintf("Node %s - ClusterCockpit", vars["nodeId"]), Config: conf, - Infos: map[string]interface{}{ - "nodeId": vars["nodeId"], - "clusterId": vars["clusterId"], - }, + Infos: infos, }) }) } diff --git a/templates/404.html b/templates/404.tmpl similarity index 87% rename from templates/404.html rename to templates/404.tmpl index c937561..1bddd58 100644 --- a/templates/404.html +++ b/templates/404.tmpl @@ -1,4 +1,4 @@ -{{template "base.html" .}} +{{template "base.tmpl" .}} {{define "content"}}
diff --git a/templates/base.html b/templates/base.html deleted file mode 100644 index 4691f30..0000000 --- a/templates/base.html +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - {{.Title}} - - - - - - - - {{block "stylesheets" .}}{{end}} - - -
-
-
- {{block "content" .}} - Whoops, you should not see this... - {{end}} -
-
-
- {{block "javascript" .}}{{end}} - - diff --git a/templates/base.tmpl b/templates/base.tmpl new file mode 100644 index 0000000..7a38de9 --- /dev/null +++ b/templates/base.tmpl @@ -0,0 +1,119 @@ + + + + + + {{.Title}} + + + + + + + + {{block "stylesheets" .}} + {{end}} + + +
+ +
+ +
+
+ {{block "content" .}} + Whoops, you should not see this... + {{end}} +
+
+ + {{block "footer" .}} + + {{end}} + + {{block "javascript" .}} + {{end}} + + diff --git a/templates/home.html b/templates/home.tmpl similarity index 100% rename from templates/home.html rename to templates/home.tmpl diff --git a/templates/login.html b/templates/login.html deleted file mode 100644 index 7f6507b..0000000 --- a/templates/login.html +++ /dev/null @@ -1,47 +0,0 @@ -{{define "content"}} -
-
-

- ClusterCockpit Login -

-
-
-
-
- {{if .Login.Error}} - - {{end}} - - {{if .Login.Info}} - - {{end}} -
-
-
-
-
-
- - -
-
- - -
- -
-
-
-
-
-
-
- -
-
-
-{{end}} diff --git a/templates/login.tmpl b/templates/login.tmpl new file mode 100644 index 0000000..bcd2462 --- /dev/null +++ b/templates/login.tmpl @@ -0,0 +1,41 @@ +{{define "navigation"}} +{{end}} + +{{define "content"}} +
+
+
+
+ {{if .Login.Error}} + + {{end}} + + {{if .Login.Info}} + + {{end}} + +
+
+

Login

+
+
+
+
+ + +
+
+ + +
+ +
+
+
+
+
+{{end}} diff --git a/templates/monitoring/analysis.html b/templates/monitoring/analysis.tmpl similarity index 100% rename from templates/monitoring/analysis.html rename to templates/monitoring/analysis.tmpl diff --git a/templates/monitoring/job.html b/templates/monitoring/job.tmpl similarity index 100% rename from templates/monitoring/job.html rename to templates/monitoring/job.tmpl diff --git a/templates/monitoring/jobs.html b/templates/monitoring/jobs.tmpl similarity index 100% rename from templates/monitoring/jobs.html rename to templates/monitoring/jobs.tmpl diff --git a/templates/monitoring/list.html b/templates/monitoring/list.tmpl similarity index 100% rename from templates/monitoring/list.html rename to templates/monitoring/list.tmpl diff --git a/templates/monitoring/node.html b/templates/monitoring/node.tmpl similarity index 100% rename from templates/monitoring/node.html rename to templates/monitoring/node.tmpl diff --git a/templates/monitoring/systems.html b/templates/monitoring/systems.tmpl similarity index 100% rename from templates/monitoring/systems.html rename to templates/monitoring/systems.tmpl diff --git a/templates/monitoring/user.html b/templates/monitoring/user.tmpl similarity index 100% rename from templates/monitoring/user.html rename to templates/monitoring/user.tmpl diff --git a/templates/templates.go b/templates/templates.go index 471cab6..95d87a6 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -27,16 +27,16 @@ type LoginPage struct { func init() { templatesDir = "./templates/" - base := template.Must(template.ParseFiles(templatesDir + "base.html")) + base := template.Must(template.ParseFiles(templatesDir + "base.tmpl")) files := []string{ - "home.html", "404.html", "login.html", - "monitoring/jobs.html", - "monitoring/job.html", - "monitoring/list.html", - "monitoring/user.html", - // "monitoring/analysis.html", - // "monitoring/systems.html", - // "monitoring/node.html", + "home.tmpl", "404.tmpl", "login.tmpl", + "monitoring/jobs.tmpl", + "monitoring/job.tmpl", + "monitoring/list.tmpl", + "monitoring/user.tmpl", + // "monitoring/analysis.tmpl", + // "monitoring/systems.tmpl", + // "monitoring/node.tmpl", } for _, file := range files { @@ -51,7 +51,7 @@ func Render(rw http.ResponseWriter, r *http.Request, file string, page *Page) { } if debugMode { - t = template.Must(template.ParseFiles(templatesDir+"base.html", templatesDir+file)) + t = template.Must(template.ParseFiles(templatesDir+"base.tmpl", templatesDir+file)) } if err := t.Execute(rw, page); err != nil {