Implement a noscript version to fix #13 #61

Open
zottelchin wants to merge 3 commits from feature/noscript into master
  1. 26
      api.go
  2. 1
      go.mod
  3. 76
      resources/noscript.gohtml
  4. 1
      web/index.html

26
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))

1
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
)

76
resources/noscript.gohtml

@ -0,0 +1,76 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Server Status</title>
<link rel="shortcut icon" href="favicon.ico">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="lib/bulma.min.css">
<!-- This site is using Eva Icons - https://github.com/akveo/eva-icons -->
<style>
img.icon { height: 1.7em; vertical-align: top; margin: -0.2em 0 -0.2em }
.has-text-white img.icon, .has-background-black img.icon { filter: invert(100%); }
.container:first-child>hr { display: none }
.footer { padding-bottom: 3rem; box-shadow: 0 100vh 0 100vh #fafafa; }
</style>
</head>
<body>
<div class="has-text-centered has-text-weight-bold" style="margin-top: 1em;">
IMPORTANT NOTICE: This is the noscript version of Chihuahua! Retrieved on {{ .Time -}}.
</div>
<main class="section" style="padding-top: 1.5em;">
{{ range .Servers}}
<section class="container" style="margin-top: 2em;" key="{{ .ID }}">
<hr>
<h2 class="subtitle" style="margin-bottom: 0.5em;"><img class="icon" src="icons/hard-drive-outline.svg"> <strong>{{ .Name }}</strong></h2>
<div class="columns" style="flex-wrap: wrap">
{{ range .Checks }}
<article class="panel column is-one-quarter" :key="{{ .ID }}">
<p style="font-weight: bold"
{{ if (eq .Result.Status 0) }}
class="panel-heading has-background-success has-text-white"
{{ end }}
{{ if (eq .Result.Status 1) }}
class="panel-heading has-background-warning"
{{ end }}
{{ if (eq .Result.Status 2) }}
class="panel-heading has-background-danger has-text-white"
{{ end }}
class="panel-heading has-background-black has-text-warning"
>
<img class="icon"
{{ if (eq .Result.Status 0) }}
src="icons/checkmark-outline.svg"
{{ end }}
{{ if (eq .Result.Status 1) }}
src="icons/alert-triangle-outline.svg"
{{ end }}
{{ if (eq .Result.Status 2) }}
src="icons/close-outline.svg"
{{ end }}
src="icons/question-mark-outline.svg"
>
{{ .Name }}
</p>
<div class="panel-block" style="padding: 0">
<pre style="white-space: pre-wrap; flex-grow: 1"><code><div>{{ if .Result.Details }}<small>{{ .Result.Details }}</small>{{ end }}{{ if .Result.Error }}<small class="has-text-danger">{{ .Result.Error }}</small>{{ end }}</div><small class="is-size-7 has-text-grey-light">{{ formatTime .Result.LastUpdate }}</small></code></pre>
</div>
</article>
{{ end }}
</div>
</section>
{{ end }}
</main>
<br>
<footer class="footer">
<div class="content has-text-centered">
<p>
<img src="logo.svg" class="icon" style="margin: -0.1em -0.2em 0.1em 0"> <strong><a href="https://codeberg.org/momar/chihuahua">Chihuahua</a> &bull; the smallest watchdog on earth</strong> by <a href="https://mo-mar.de">Moritz Marquardt</a>
</p>
</div>
</footer>
</body>
</html>

1
web/index.html

@ -17,6 +17,7 @@
</style>
</head>
<body>
<noscript>Your browser does not support JavaScript! <br> To view checks without javascript, visit the <a href="/noscript">noscript version</a>.</noscript>
<main class="section" id="app">
<section class="container" style="margin-top: 2em" v-for="server in servers" :key="server.ID">
<hr>

Loading…
Cancel
Save