Bootstrap reanimator chart viewer
This commit is contained in:
57
web/embed.go
Normal file
57
web/embed.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed templates/* static/*
|
||||
var content embed.FS
|
||||
|
||||
var pageTemplate = template.Must(template.New("view.html").Funcs(template.FuncMap{
|
||||
"statusClass": statusClass,
|
||||
"joinLines": joinLines,
|
||||
}).ParseFS(content, "templates/view.html"))
|
||||
|
||||
func Render(data any) ([]byte, error) {
|
||||
var out strings.Builder
|
||||
if err := pageTemplate.ExecuteTemplate(&out, "view.html", data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(out.String()), nil
|
||||
}
|
||||
|
||||
func Static() http.Handler {
|
||||
sub, err := fs.Sub(content, "static")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return http.FileServer(http.FS(sub))
|
||||
}
|
||||
|
||||
func statusClass(value string) string {
|
||||
switch strings.ToUpper(strings.TrimSpace(value)) {
|
||||
case "OK":
|
||||
return "status-ok"
|
||||
case "WARNING":
|
||||
return "status-warning"
|
||||
case "CRITICAL":
|
||||
return "status-critical"
|
||||
case "UNKNOWN":
|
||||
return "status-unknown"
|
||||
case "EMPTY":
|
||||
return "status-empty"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func joinLines(value string) []string {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(value, "\n")
|
||||
}
|
||||
Reference in New Issue
Block a user