From 3c9a0052f2d8dd2af99f7515521e3a7d39c6f743 Mon Sep 17 00:00:00 2001 From: Hector Date: Fri, 24 Sep 2021 15:53:10 +0100 Subject: [PATCH 1/2] feat: render basic html page at root url Add a new request handler for the root URL (`/`) to render a simple HTML page with a link to the metrics page. This follows the convention of other metric exporters. --- src/exporter.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/exporter.go b/src/exporter.go index 6cc8793..59874e6 100644 --- a/src/exporter.go +++ b/src/exporter.go @@ -40,6 +40,15 @@ func main() { exporter := export.NewExporter(appSettings, version) prometheus.MustRegister(exporter) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(` + Fail2Ban Exporter + +

Node Exporter

+

Metrics

+ + `)) + }) http.Handle(metricsPath, promhttp.Handler()) log.Printf("metrics available at '%s'", metricsPath) From dcce3cac817949853d01b77774a99502126e6912 Mon Sep 17 00:00:00 2001 From: Hector Date: Sat, 25 Sep 2021 22:17:24 +0100 Subject: [PATCH 2/2] move handler to function Move the http handler to a new function. Add error handling. Fix exporter name in root page. --- src/exporter.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/exporter.go b/src/exporter.go index 59874e6..b06ca2c 100644 --- a/src/exporter.go +++ b/src/exporter.go @@ -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) } +func rootHtmlHandler(w http.ResponseWriter, r *http.Request) { + _, err := w.Write([]byte( + ` + Fail2Ban Exporter + +

Fail2Ban Exporter

+

Metrics

+ + `)) + if err != nil { + log.Printf("error handling root url: %v", err) + w.WriteHeader(http.StatusInternalServerError) + } +} + func main() { appSettings := cfg.Parse() if appSettings.VersionMode { @@ -40,15 +55,7 @@ func main() { exporter := export.NewExporter(appSettings, version) prometheus.MustRegister(exporter) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(` - Fail2Ban Exporter - -

Node Exporter

-

Metrics

- - `)) - }) + http.HandleFunc("/", rootHtmlHandler) http.Handle(metricsPath, promhttp.Handler()) log.Printf("metrics available at '%s'", metricsPath)