Skip to content

Commit 546ee64

Browse files
committed
Add a simple UI.
1 parent 2a4386a commit 546ee64

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

cmd/jiralert/content.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"html/template"
6+
"net/http"
7+
8+
"github.com/free/jiralert"
9+
)
10+
11+
const (
12+
docsUrl = "https://github.com/free/jiralert#readme"
13+
templates = `
14+
{{ define "page" -}}
15+
<html>
16+
<head>
17+
<title>JIRAlert</title>
18+
<style type="text/css">
19+
body { margin: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.42857143; color: #333; background-color: #fff; }
20+
.navbar { display: flex; background-color: #222; margin: 0; border-width: 0 0 1px; border-style: solid; border-color: #080808; }
21+
.navbar > * { margin: 0; padding: 15px; }
22+
.navbar * { line-height: 20px; color: #9d9d9d; }
23+
.navbar a { text-decoration: none; }
24+
.navbar a:hover, .navbar a:focus { color: #fff; }
25+
.navbar-header { font-size: 18px; }
26+
body > * { margin: 15px; padding: 0; }
27+
pre { padding: 10px; font-size: 13px; background-color: #f5f5f5; border: 1px solid #ccc; }
28+
h1, h2 { font-weight: 500; }
29+
a { color: #337ab7; }
30+
a:hover, a:focus { color: #23527c; }
31+
</style>
32+
</head>
33+
<body>
34+
<div class="navbar">
35+
<div class="navbar-header"><a href="/">JIRAlert</a></div>
36+
<div><a href="/config">Configuration</a></div>
37+
<div><a href="/metrics">Metrics</a></div>
38+
<div><a href="/debug/pprof">Profiling</a></div>
39+
<div><a href="{{ .DocsUrl }}">Help</a></div>
40+
</div>
41+
{{template "content" .}}
42+
</body>
43+
</html>
44+
{{- end }}
45+
46+
{{ define "content.home" -}}
47+
<p>This is <a href="{{ .DocsUrl }}">JIRAlert</a>, a
48+
<a href="https://prometheus.io/docs/alerting/configuration/#webhook_config">webhook receiver</a> for
49+
<a href="https://prometheus.io/docs/alerting/alertmanager/">Prometheus Alertmanager</a>.
50+
{{- end }}
51+
52+
{{ define "content.config" -}}
53+
<h2>Configuration</h2>
54+
<pre>{{ .Config }}</pre>
55+
{{- end }}
56+
57+
{{ define "content.error" -}}
58+
<h2>Error</h2>
59+
<pre>{{ .Err }}</pre>
60+
{{- end }}
61+
`
62+
)
63+
64+
type tdata struct {
65+
DocsUrl string
66+
67+
// `/config` only
68+
Config string
69+
70+
// `/error` only
71+
Err error
72+
}
73+
74+
var (
75+
allTemplates = template.Must(template.New("").Parse(templates))
76+
homeTemplate = pageTemplate("home")
77+
configTemplate = pageTemplate("config")
78+
errorTemplate = pageTemplate("error")
79+
)
80+
81+
func pageTemplate(name string) *template.Template {
82+
pageTemplate := fmt.Sprintf(`{{define "content"}}{{template "content.%s" .}}{{end}}{{template "page" .}}`, name)
83+
return template.Must(template.Must(allTemplates.Clone()).Parse(pageTemplate))
84+
}
85+
86+
// HomeHandlerFunc is the HTTP handler for the home page (`/`).
87+
func HomeHandlerFunc() func(http.ResponseWriter, *http.Request) {
88+
return func(w http.ResponseWriter, r *http.Request) {
89+
homeTemplate.Execute(w, &tdata{
90+
DocsUrl: docsUrl,
91+
})
92+
}
93+
}
94+
95+
// ConfigHandlerFunc is the HTTP handler for the `/config` page. It outputs the configuration marshaled in YAML format.
96+
func ConfigHandlerFunc(config *jiralert.Config) func(http.ResponseWriter, *http.Request) {
97+
return func(w http.ResponseWriter, r *http.Request) {
98+
configTemplate.Execute(w, &tdata{
99+
DocsUrl: docsUrl,
100+
Config: config.String(),
101+
})
102+
}
103+
}
104+
105+
// HandleError is an error handler that other handlers defer to in case of error. It is important to not have written
106+
// anything to w before calling HandleError(), or the 500 status code won't be set (and the content might be mixed up).
107+
func HandleError(err error, metricsPath string, w http.ResponseWriter, r *http.Request) {
108+
w.WriteHeader(http.StatusInternalServerError)
109+
errorTemplate.Execute(w, &tdata{
110+
DocsUrl: docsUrl,
111+
Err: err,
112+
})
113+
}

cmd/jiralert/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func main() {
9898
requestTotal.WithLabelValues(conf.Name, "200").Inc()
9999
})
100100

101+
http.HandleFunc("/", HomeHandlerFunc())
102+
http.HandleFunc("/config", ConfigHandlerFunc(config))
103+
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "OK", http.StatusOK) })
101104
http.Handle("/metrics", promhttp.Handler())
102105

103106
if os.Getenv("PORT") != "" {

0 commit comments

Comments
 (0)