From 82b8e8c284a573593186129f9fa38dd8b97edad7 Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Thu, 22 Jun 2023 16:26:09 +0200 Subject: [PATCH] Fix bug in SearchBar Handler Introduce Message boxes Incomplete and needs cleanup --- cmd/cc-backend/main.go | 2 +- internal/routerConfig/routes.go | 37 +++++++++++++++------------ web/templates/message.tmpl | 21 +++++++++++++++ web/web.go | 45 ++++++++++++++++++++++++++++++--- 4 files changed, 85 insertions(+), 20 deletions(-) create mode 100644 web/templates/message.tmpl diff --git a/cmd/cc-backend/main.go b/cmd/cc-backend/main.go index f9868e6..164d7f1 100644 --- a/cmd/cc-backend/main.go +++ b/cmd/cc-backend/main.go @@ -316,7 +316,7 @@ func main() { // Send a searchId and then reply with a redirect to a user, or directly send query to job table for jobid and project. secured.HandleFunc("/search", func(rw http.ResponseWriter, r *http.Request) { - routerConfig.HandleSearchBar(rw, r, api) + routerConfig.HandleSearchBar(rw, r) }) // Mount all /monitoring/... and /api/... routes. diff --git a/internal/routerConfig/routes.go b/internal/routerConfig/routes.go index 94d8259..9000c18 100644 --- a/internal/routerConfig/routes.go +++ b/internal/routerConfig/routes.go @@ -12,7 +12,6 @@ import ( "strings" "time" - "github.com/ClusterCockpit/cc-backend/internal/api" "github.com/ClusterCockpit/cc-backend/internal/auth" "github.com/ClusterCockpit/cc-backend/internal/graph/model" "github.com/ClusterCockpit/cc-backend/internal/repository" @@ -270,8 +269,9 @@ func SetupRoutes(router *mux.Router, version string, hash string, buildTime stri } } -func HandleSearchBar(rw http.ResponseWriter, r *http.Request, api *api.RestApi) { +func HandleSearchBar(rw http.ResponseWriter, r *http.Request) { if search := r.URL.Query().Get("searchId"); search != "" { + repo := repository.GetJobRepository() user := auth.GetUser(r.Context()) splitSearch := strings.Split(search, ":") @@ -287,10 +287,11 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request, api *api.RestApi) if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) { http.Redirect(rw, r, "/monitoring/users/?user="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusFound) } else { - http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Users: Redirect to Tablequery + web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Missing Access Rights"}) + // web.RenderMessage(rw, "error", "Missing access rights!") } case "name": - usernames, _ := api.JobRepository.FindColumnValues(user, strings.Trim(splitSearch[1], " "), "user", "username", "name") + usernames, _ := repo.FindColumnValues(user, strings.Trim(splitSearch[1], " "), "user", "username", "name") if len(usernames) != 0 { joinedNames := strings.Join(usernames, "&user=") http.Redirect(rw, r, "/monitoring/users/?user="+joinedNames, http.StatusFound) @@ -298,23 +299,27 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request, api *api.RestApi) if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) { http.Redirect(rw, r, "/monitoring/users/?user=NoUserNameFound", http.StatusPermanentRedirect) } else { - http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Users: Redirect to Tablequery + web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Missing Access Rights"}) + // web.RenderMessage(rw, "error", "Missing access rights!") } } default: - log.Warnf("Searchbar type parameter '%s' unknown", strings.Trim(splitSearch[0], " ")) - http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Unknown: Redirect to Tablequery + web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: fmt.Sprintf("Unknown search term %s", strings.Trim(splitSearch[0], " "))}) + // web.RenderMessage(rw, "error", fmt.Sprintf("Unknown search term %s", strings.Trim(splitSearch[0], " "))) } } else if len(splitSearch) == 1 { - username, project, jobname, _ := api.JobRepository.FindUserOrProjectOrJobname(user, strings.Trim(search, " ")) + username, project, jobname, err := repo.FindUserOrProjectOrJobname(user, strings.Trim(search, " ")) + // err := fmt.Errorf("Blabla") /* Causes 'http: superfluous response.WriteHeader call' causing SSL error and frontend crash: Cause unknown*/ - // if err != nil { - // log.Errorf("Error while searchbar best guess: %v", err.Error()) - // http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Unknown: Redirect to Tablequery - //} + if err != nil { + web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "No search result"}) + return + // web.RenderMessage(rw, "info", "Search with no result") + // log.Errorf("Error while searchbar best guess: %v", err.Error()) + } if username != "" { http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusFound) // User: Redirect to user page @@ -327,11 +332,11 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request, api *api.RestApi) } } else { - log.Warnf("Searchbar query parameters malformed: %v", search) - http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Unknown: Redirect to Tablequery + web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Searchbar query parameters malformed"}) + // web.RenderMessage(rw, "warn", "Searchbar query parameters malformed") } - } else { - http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusTemporaryRedirect) + web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Empty search"}) + // web.RenderMessage(rw, "warn", "Empty search") } } diff --git a/web/templates/message.tmpl b/web/templates/message.tmpl new file mode 100644 index 0000000..72e5d69 --- /dev/null +++ b/web/templates/message.tmpl @@ -0,0 +1,21 @@ +{{define "navigation"}} +
+ +
+{{end}} + +{{define "content"}} +
+
+ +
+
+{{end}} diff --git a/web/web.go b/web/web.go index 3547b85..04293d1 100644 --- a/web/web.go +++ b/web/web.go @@ -96,8 +96,7 @@ type Page struct { func RenderTemplate(rw http.ResponseWriter, r *http.Request, file string, page *Page) { t, ok := templates[file] if !ok { - log.Fatalf("WEB/WEB > template '%s' not found", file) - panic("template not found") + log.Errorf("WEB/WEB > template '%s' not found", file) } if page.Clusters == nil { @@ -106,7 +105,47 @@ func RenderTemplate(rw http.ResponseWriter, r *http.Request, file string, page * } } - log.Infof("Page config : %v\n", page.Config) + log.Debugf("Page config : %v\n", page.Config) + if err := t.Execute(rw, page); err != nil { + log.Errorf("Template error: %s", err.Error()) + } +} + +type Message struct { + Title string + Type string + Message string + Icon string +} + +func RenderMessage(rw http.ResponseWriter, msgType string, msg string) { + var page Message + log.Info("render message template") + + switch msgType { + case "success": + page.Title = "Success" + page.Type = "alert-success" + case "info": + page.Title = "Info" + page.Type = "alert-info" + case "warn": + page.Title = "Warning" + page.Type = "alert-warning" + case "error": + page.Title = "Error" + page.Type = "alert-danger" + default: + page.Title = "Message" + page.Type = "alert-secondary" + } + t, ok := templates["message.tmpl"] + if !ok { + log.Error("WEB/WEB > template message.tmpl not found") + } + page.Message = msg + rw.Header().Add("Content-Type", "text/html; charset=utf-8") + if err := t.Execute(rw, page); err != nil { log.Errorf("Template error: %s", err.Error()) }