From dcce3cac817949853d01b77774a99502126e6912 Mon Sep 17 00:00:00 2001 From: Hector Date: Sat, 25 Sep 2021 22:17:24 +0100 Subject: [PATCH] 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)