Merge origin/main into main
This commit is contained in:
commit
161bf5e8cb
20 changed files with 1592 additions and 116 deletions
|
|
@ -10,13 +10,24 @@ import (
|
|||
"time"
|
||||
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/config"
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/history"
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/store"
|
||||
)
|
||||
|
||||
// Server serves the single configured HTTP status and health check endpoint.
|
||||
const Version = "v1.1"
|
||||
|
||||
// StatsQuerier is the read interface for long-term aggregated check statistics.
|
||||
type StatsQuerier interface {
|
||||
DailyStats(monitorID string, days int) ([]history.DailyStat, error)
|
||||
HourlyStats(monitorID string, hours int) ([]history.HourlyStat, error)
|
||||
}
|
||||
|
||||
// Server serves the single configured HTTP status and health check endpoint,
|
||||
// plus the per-monitor historical stats page at /monitor.
|
||||
type Server struct {
|
||||
cfg *config.Config
|
||||
store *store.Store
|
||||
stats StatsQuerier
|
||||
}
|
||||
|
||||
// New creates a new single-endpoint HTTP server.
|
||||
|
|
@ -27,8 +38,22 @@ func New(cfg *config.Config, st *store.Store) *Server {
|
|||
}
|
||||
}
|
||||
|
||||
// SetStats attaches a long-term stats store used by the /monitor page.
|
||||
func (s *Server) SetStats(q StatsQuerier) {
|
||||
s.stats = q
|
||||
}
|
||||
|
||||
// ServeHTTP handles incoming HTTP requests strictly on the configured single endpoint.
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/favicon.svg" {
|
||||
s.serveFavicon(w, r)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/monitor" || strings.HasPrefix(r.URL.Path, "/monitor/") {
|
||||
s.serveMonitor(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
endpoint := s.cfg.Server.Endpoint
|
||||
if endpoint == "" {
|
||||
endpoint = "/"
|
||||
|
|
@ -276,7 +301,7 @@ func (s *Server) renderHTML(snap store.Snapshot) ([]byte, error) {
|
|||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := pageTemplate.Execute(&buf, vd); err != nil {
|
||||
if err := templates.ExecuteTemplate(&buf, "page", vd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
|
|
@ -429,22 +454,19 @@ func safeCSSValue(v string) template.CSS {
|
|||
return template.CSS(cssSanitizer.Replace(strings.TrimSpace(v)))
|
||||
}
|
||||
|
||||
var pageTemplate = template.Must(template.New("status").Funcs(template.FuncMap{
|
||||
var templates = template.Must(template.New("status").Funcs(template.FuncMap{
|
||||
"upper": strings.ToUpper,
|
||||
"safeCSS": safeCSSValue,
|
||||
"Version": func() string {
|
||||
return Version
|
||||
},
|
||||
"fmtTime": func(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return "-"
|
||||
}
|
||||
return t.UTC().Format("2006-01-02 15:04:05 UTC")
|
||||
},
|
||||
}).Parse(`<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{.Title}}</title>
|
||||
<style>
|
||||
}).Parse(`{{define "pagecss"}}<style>
|
||||
:root {
|
||||
--bg: {{safeCSS .Theme.BgColor}};
|
||||
--card: {{safeCSS .Theme.CardBgColor}};
|
||||
|
|
@ -612,7 +634,14 @@ a:hover { color: var(--accent); }
|
|||
color: var(--muted);
|
||||
}
|
||||
{{.CustomCSS}}
|
||||
</style>
|
||||
</style>{{end}}{{define "page"}}<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
|
||||
<title>{{.Title}}</title>
|
||||
{{template "pagecss" .}}
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
|
|
@ -667,11 +696,8 @@ a:hover { color: var(--accent); }
|
|||
<div class="mon" data-monitor-id="{{.Monitor.ID}}">
|
||||
<div class="mon-top">
|
||||
<div class="mon-left">
|
||||
{{- if .Monitor.Link}}
|
||||
<a class="mon-name" href="{{.Monitor.Link}}" target="_blank" rel="noopener">{{.Monitor.Name}}</a>
|
||||
{{- else}}
|
||||
<span class="mon-name">{{.Monitor.Name}}</span>
|
||||
{{- end}}
|
||||
<a class="mon-name" href="/monitor?id={{.Monitor.ID}}">{{.Monitor.Name}}</a>
|
||||
{{- if .Monitor.Link}}<a class="mon-ext" href="{{.Monitor.Link}}" target="_blank" rel="noopener" title="External link">↗</a>{{end}}
|
||||
{{- if $disp.ShowMethod}}<span class="mon-method">{{upper .Monitor.Method}}</span>{{end}}
|
||||
{{- if and $disp.ShowTarget .Monitor.Target}}<span class="mon-target">{{.Monitor.Target}}</span>{{end}}
|
||||
</div>
|
||||
|
|
@ -714,7 +740,7 @@ a:hover { color: var(--accent); }
|
|||
{{- end}}
|
||||
|
||||
<footer class="footer">
|
||||
Powered by <a href="https://git.gsdistance.org/tyuXX/RUD" target="_blank" rel="noopener">R u ded?</a>
|
||||
Powered by <a href="https://git.gsdistance.org/tyuXX/RUD" target="_blank" rel="noopener">R u ded?</a> {{Version}}
|
||||
</footer>
|
||||
</div>
|
||||
{{- if gt .RefreshMs 0}}
|
||||
|
|
@ -813,5 +839,97 @@ a:hover { color: var(--accent); }
|
|||
</script>
|
||||
{{- end}}
|
||||
</body>
|
||||
</html>
|
||||
</html>{{end}}
|
||||
|
||||
{{define "monitor"}}<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
|
||||
<title>{{.Title}}</title>
|
||||
{{template "pagecss" .}}
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<header class="header header-left">
|
||||
<div>
|
||||
{{- if .Monitor}}<h1>{{.Monitor.Name}}</h1>
|
||||
<div class="subtitle">{{upper .Monitor.Method}}{{if .Monitor.Target}} — {{.Monitor.Target}}{{end}}</div>
|
||||
{{- else if .Index}}<h1>Monitors</h1>
|
||||
<div class="subtitle">Select a monitor to view its history</div>
|
||||
{{- else}}<h1>{{.MonitorID}}</h1>
|
||||
<div class="subtitle">Monitor not found</div>{{end}}
|
||||
</div>
|
||||
<div class="header-meta"><a href="{{.BackLink}}">← status page</a></div>
|
||||
</header>
|
||||
|
||||
{{- if .Index}}
|
||||
<section class="group">
|
||||
<div class="monitors-list">
|
||||
{{- range .Monitors}}
|
||||
<div class="mon">
|
||||
<div class="mon-top">
|
||||
<div class="mon-left">
|
||||
<a class="mon-name" href="/monitor?id={{.ID}}">{{.Name}}</a>
|
||||
<span class="mon-method">{{upper .Method}}</span>
|
||||
{{- if .Target}}<span class="mon-target">{{.Target}}</span>{{end}}
|
||||
</div>
|
||||
<div class="mon-right">
|
||||
<span class="badge" data-state="{{.State}}">{{.State}}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{- end}}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{{- else if .Monitor}}
|
||||
<section class="banner" data-state="{{.Monitor.State}}">
|
||||
<div class="banner-title">{{upper .Monitor.State}}</div>
|
||||
<div class="banner-meta">
|
||||
<span>last checked {{fmtTime .Monitor.LastCheckedAt}}</span>
|
||||
<span>{{printf "%.1fms" .Monitor.LatencyMs}} latency</span>
|
||||
<span>{{printf "%.2f%%" .Monitor.UptimePct}} uptime</span>
|
||||
{{- if .Monitor.LastMessage}}<span>{{.Monitor.LastMessage}}</span>{{end}}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<div class="cards">
|
||||
<div class="card"><div class="card-label">CHECKS ({{.Days}}D)</div><div class="card-val">{{.RangeChecks}}</div></div>
|
||||
<div class="card"><div class="card-label">UPTIME ({{.Days}}D)</div><div class="card-val">{{printf "%.2f%%" .RangeUptime}}</div></div>
|
||||
<div class="card"><div class="card-label">AVG LATENCY</div><div class="card-val">{{printf "%.1fms" .RangeAvgLat}}</div></div>
|
||||
<div class="card"><div class="card-label">FAILURES ({{.Days}}D)</div><div class="card-val" style="color:var(--down)">{{.RangeDown}}</div></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{{- if .HasStats}}
|
||||
<section class="group">
|
||||
<div class="group-hdr"><div class="group-title">Daily uptime — last {{.Days}} days</div></div>
|
||||
{{.UptimeChart}}
|
||||
</section>
|
||||
<section class="group">
|
||||
<div class="group-hdr"><div class="group-title">Avg response time — last {{.Days}} days</div></div>
|
||||
{{.LatencyChart}}
|
||||
</section>
|
||||
<section class="group">
|
||||
<div class="group-hdr"><div class="group-title">Hourly uptime — {{.HourlyLabel}}</div></div>
|
||||
{{.HourlyChart}}
|
||||
</section>
|
||||
{{- else}}
|
||||
<section class="custom-block" data-style="info"><div>No historical stats recorded yet for this monitor.</div></section>
|
||||
{{- end}}
|
||||
{{- if .Monitor.Description}}<section class="custom-block" data-style="neutral"><div>{{.Monitor.Description}}</div></section>{{end}}
|
||||
|
||||
{{- else}}
|
||||
<section class="custom-block" data-style="warning"><div>Unknown monitor <code>{{.MonitorID}}</code>.</div></section>
|
||||
{{- end}}
|
||||
|
||||
<footer class="footer">
|
||||
Powered by <a href="https://git.gsdistance.org/tyuXX/RUD" target="_blank" rel="noopener">R u ded?</a> {{Version}}
|
||||
</footer>
|
||||
</div>
|
||||
</body>
|
||||
</html>{{end}}
|
||||
`))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue