Unify and cleanup message template

This commit is contained in:
Jan Eitzinger 2023-06-22 18:09:40 +02:00
parent 82b8e8c284
commit 8eda4b306d
5 changed files with 25 additions and 80 deletions

View File

@ -275,7 +275,8 @@ func main() {
rw.WriteHeader(http.StatusUnauthorized) rw.WriteHeader(http.StatusUnauthorized)
web.RenderTemplate(rw, r, "login.tmpl", &web.Page{ web.RenderTemplate(rw, r, "login.tmpl", &web.Page{
Title: "Login failed - ClusterCockpit", Title: "Login failed - ClusterCockpit",
Error: err.Error(), MsgType: "alert-warning",
Message: err.Error(),
Build: buildInfo, Build: buildInfo,
}) })
})).Methods(http.MethodPost) })).Methods(http.MethodPost)
@ -285,7 +286,8 @@ func main() {
rw.WriteHeader(http.StatusOK) rw.WriteHeader(http.StatusOK)
web.RenderTemplate(rw, r, "login.tmpl", &web.Page{ web.RenderTemplate(rw, r, "login.tmpl", &web.Page{
Title: "Bye - ClusterCockpit", Title: "Bye - ClusterCockpit",
Info: "Logout sucessful", MsgType: "alert-info",
Message: "Logout successful",
Build: buildInfo, Build: buildInfo,
}) })
}))).Methods(http.MethodPost) }))).Methods(http.MethodPost)
@ -300,7 +302,8 @@ func main() {
rw.WriteHeader(http.StatusUnauthorized) rw.WriteHeader(http.StatusUnauthorized)
web.RenderTemplate(rw, r, "login.tmpl", &web.Page{ web.RenderTemplate(rw, r, "login.tmpl", &web.Page{
Title: "Authentication failed - ClusterCockpit", Title: "Authentication failed - ClusterCockpit",
Error: err.Error(), MsgType: "alert-danger",
Message: err.Error(),
Build: buildInfo, Build: buildInfo,
}) })
}) })

View File

@ -287,8 +287,7 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request) {
if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) { 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) http.Redirect(rw, r, "/monitoring/users/?user="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusFound)
} else { } else {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Missing Access Rights"}) web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Error", MsgType: "alert-danger", Message: "Missing Access Rights"})
// web.RenderMessage(rw, "error", "Missing access rights!")
} }
case "name": case "name":
usernames, _ := repo.FindColumnValues(user, strings.Trim(splitSearch[1], " "), "user", "username", "name") usernames, _ := repo.FindColumnValues(user, strings.Trim(splitSearch[1], " "), "user", "username", "name")
@ -299,26 +298,18 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request) {
if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) { if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) {
http.Redirect(rw, r, "/monitoring/users/?user=NoUserNameFound", http.StatusPermanentRedirect) http.Redirect(rw, r, "/monitoring/users/?user=NoUserNameFound", http.StatusPermanentRedirect)
} else { } else {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Missing Access Rights"}) web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Error", MsgType: "alert-danger", Message: "Missing Access Rights"})
// web.RenderMessage(rw, "error", "Missing access rights!")
} }
} }
default: default:
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: fmt.Sprintf("Unknown search term %s", strings.Trim(splitSearch[0], " "))}) web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warning", MsgType: "alert-warning", Message: 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 { } else if len(splitSearch) == 1 {
username, project, jobname, err := repo.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 { if err != nil {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "No search result"}) web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Info", MsgType: "alert-info", Message: "Search without result"})
return return
// web.RenderMessage(rw, "info", "Search with no result")
// log.Errorf("Error while searchbar best guess: %v", err.Error())
} }
if username != "" { if username != "" {
@ -330,13 +321,10 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request) {
} else { } else {
http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(strings.Trim(search, " ")), http.StatusFound) // No Result: Probably jobId http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(strings.Trim(search, " ")), http.StatusFound) // No Result: Probably jobId
} }
} else { } else {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Searchbar query parameters malformed"}) web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Error", MsgType: "alert-danger", Message: "Searchbar query parameters malformed"})
// web.RenderMessage(rw, "warn", "Searchbar query parameters malformed")
} }
} else { } else {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Empty search"}) web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warning", MsgType: "alert-warning", Message: "Empty search"})
// web.RenderMessage(rw, "warn", "Empty search")
} }
} }

View File

@ -17,15 +17,9 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-4 mx-auto"> <div class="col-4 mx-auto">
{{if .Error}} {{if .MsgType}}
<div class="alert alert-warning" role="alert"> <div class="alert {{.MsgType}}" role="alert">
{{.Error}} {{.Message}}
</div>
{{end}}
{{if .Info}}
<div class="alert alert-success" role="alert">
{{.Info}}
</div> </div>
{{end}} {{end}}

View File

@ -13,8 +13,8 @@
{{define "content"}} {{define "content"}}
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<div class="alert alert-info" role="alert"> <div class="alert {{.MsgType}}" role="alert">
{{.Info}} {{.Message}}
</div> </div>
</div> </div>
</div> </div>

View File

@ -82,8 +82,8 @@ type Build struct {
type Page struct { type Page struct {
Title string // Page title Title string // Page title
Error string // For generic use (e.g. the exact error message on /login) MsgType string // For generic use in message boxes
Info string // For generic use (e.g. "Logout successfull" on /login) Message string // For generic use in message boxes
User auth.User // Information about the currently logged in user (Full User Info) User auth.User // Information about the currently logged in user (Full User Info)
Roles map[string]auth.Role // Available roles for frontend render checks Roles map[string]auth.Role // Available roles for frontend render checks
Build Build // Latest information about the application Build Build // Latest information about the application
@ -110,43 +110,3 @@ func RenderTemplate(rw http.ResponseWriter, r *http.Request, file string, page *
log.Errorf("Template error: %s", err.Error()) 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())
}
}