2023-06-21 10:58:43 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-06-21 11:09:39 +00:00
|
|
|
"time"
|
2023-06-21 10:58:43 +00:00
|
|
|
|
|
|
|
"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() {
|
2023-06-21 11:09:39 +00:00
|
|
|
httpServer := &http.Server{
|
|
|
|
Addr: appSettings.MetricsAddress,
|
|
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
IdleTimeout: 30 * time.Second,
|
|
|
|
}
|
|
|
|
svrErr <- httpServer.ListenAndServe()
|
2023-06-21 10:58:43 +00:00
|
|
|
}()
|
|
|
|
log.Print("ready")
|
|
|
|
return svrErr
|
|
|
|
}
|