diff --git a/api.go b/api.go index 4deebee..4c569ce 100644 --- a/api.go +++ b/api.go @@ -4,6 +4,7 @@ package chihuahua import ( "github.com/rs/zerolog/log" + "html/template" "io/ioutil" "net/http" "net/url" @@ -146,8 +147,33 @@ func Api(cfg *Config) { c.String(200, strings.ReplaceAll(string(setupScript), "[CHIHUAHUA_PUBLIC_KEYS]", keyList)) } + noScriptFile, err := pkger.Open("codeberg.org/momar/chihuahua:/resources/noscript.gohtml") + if err != nil { + log.Error().Str("caller", "api").Err(err).Msg("couldn't open noscript.gohtml from web resources") + os.Exit(1) + } + noScript, err := ioutil.ReadAll(noScriptFile) + if err != nil { + log.Error().Str("caller", "api").Err(err).Msg("couldn't read noscript.gohtml from web resources") + os.Exit(1) + } + noScriptTemplate, err := template.New("noscript").Funcs(template.FuncMap{ + "formatTime": func(t time.Time) string {return t.Format("2006-01-02 15:05:05")}, + }).Parse(string(noScript)) + if err != nil { + log.Error().Str("caller", "api").Err(err).Msg("couldn't parse noscript.gohtml from web resources") + os.Exit(1) + } + app := gin.New() app.Use(gin.Recovery()) + app.SetHTMLTemplate(noScriptTemplate) + app.GET("/noscript", func(c *gin.Context) { + c.HTML(200, "noscript", gin.H{ + "Time": time.Now().Format("02.01.2006 15:04:05"), + "Servers": cfg.Servers, + }) + }) app.GET("/setup.sh", getSetupScript) app.GET("/checks", GetCheck(cfg)) app.GET("/checks/*id", GetCheck(cfg)) diff --git a/go.mod b/go.mod index 39cc6a2..8667c60 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,5 @@ require ( github.com/teris-io/cli v1.0.1 golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6 // indirect golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect - golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 // indirect gopkg.in/yaml.v2 v2.2.8 // indirect ) diff --git a/resources/noscript.gohtml b/resources/noscript.gohtml new file mode 100644 index 0000000..fee849b --- /dev/null +++ b/resources/noscript.gohtml @@ -0,0 +1,76 @@ + + + + Server Status + + + + + + + + + + +
+ IMPORTANT NOTICE: This is the noscript version of Chihuahua! Retrieved on {{ .Time -}}. +
+
+ {{ range .Servers}} +
+
+

{{ .Name }}

+
+ {{ range .Checks }} +
+

+ + {{ .Name }} +

+
+
{{ if .Result.Details }}{{ .Result.Details }}{{ end }}{{ if .Result.Error }}{{ .Result.Error }}{{ end }}
{{ formatTime .Result.LastUpdate }}
+
+
+ {{ end }} +
+
+ {{ end }} +
+ +
+ + + \ No newline at end of file diff --git a/web/index.html b/web/index.html index d130a89..cd0c633 100644 --- a/web/index.html +++ b/web/index.html @@ -17,6 +17,7 @@ +