Merge branch 'render-basic-root-html-page-with-link-to-metrics' into 'main'

Render basic root html page with link to metrics

See merge request hectorjsmith/fail2ban-prometheus-exporter!35
This commit is contained in:
Hector 2021-09-25 21:23:29 +00:00
commit 51c1157d21

View File

@ -28,6 +28,21 @@ func printAppVersion() {
fmt.Printf(" build date: %s\r\n commit hash: %s\r\n built by: %s\r\n", date, commit, builtBy) fmt.Printf(" build date: %s\r\n commit hash: %s\r\n built by: %s\r\n", date, commit, builtBy)
} }
func rootHtmlHandler(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(
`<html>
<head><title>Fail2Ban Exporter</title></head>
<body>
<h1>Fail2Ban Exporter</h1>
<p><a href="` + metricsPath + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
log.Printf("error handling root url: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func main() { func main() {
appSettings := cfg.Parse() appSettings := cfg.Parse()
if appSettings.VersionMode { if appSettings.VersionMode {
@ -40,6 +55,7 @@ func main() {
exporter := export.NewExporter(appSettings, version) exporter := export.NewExporter(appSettings, version)
prometheus.MustRegister(exporter) prometheus.MustRegister(exporter)
http.HandleFunc("/", rootHtmlHandler)
http.Handle(metricsPath, promhttp.Handler()) http.Handle(metricsPath, promhttp.Handler())
log.Printf("metrics available at '%s'", metricsPath) log.Printf("metrics available at '%s'", metricsPath)