11c4b26c0b
* Move code setting up the HTTP server to it's own package * This helps clean up the `main` function and make it easier to read * Rename the `BasicAuthMiddleware` to remove reference to Basic since it can now handle any type of auth type https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/merge_requests/90
33 lines
753 B
Go
33 lines
753 B
Go
package server
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/textfile"
|
|
)
|
|
|
|
func StartServer(
|
|
appSettings *cfg.AppSettings,
|
|
textFileCollector *textfile.Collector,
|
|
) chan error {
|
|
http.HandleFunc("/", AuthMiddleware(
|
|
rootHtmlHandler,
|
|
appSettings.AuthProvider,
|
|
))
|
|
http.HandleFunc(metricsPath, AuthMiddleware(
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
metricHandler(w, r, textFileCollector)
|
|
},
|
|
appSettings.AuthProvider,
|
|
))
|
|
log.Printf("metrics available at '%s'", metricsPath)
|
|
|
|
svrErr := make(chan error)
|
|
go func() {
|
|
svrErr <- http.ListenAndServe(appSettings.MetricsAddress, nil)
|
|
}()
|
|
log.Print("ready")
|
|
return svrErr
|
|
}
|