From a287863799656c91747530f247259911b2e698bc Mon Sep 17 00:00:00 2001 From: Emile Date: Wed, 29 Jan 2020 20:12:02 +0100 Subject: setup the http server in http.go --- src/http.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/http.go') diff --git a/src/http.go b/src/http.go index 47f0d75..1ff432b 100644 --- a/src/http.go +++ b/src/http.go @@ -2,10 +2,33 @@ package main import ( "fmt" + "log" "net/http" "strings" + + "github.com/gorilla/mux" ) +func setupHTTPServer(config config) { + + // start the http server logging the metrics + log.Printf("Starting HTTP metrics listener on port %d", config.httpPort) + + r := mux.NewRouter() + r.HandleFunc("/", indexHandler) + r.HandleFunc("/metrics", metricsHandler) + r.HandleFunc("/locations", locationHandlerEndpoint) + + // start the http server exposing the metrics and the locations + httpPortString := fmt.Sprintf("%s:%d", config.bindIP, config.httpPort) + listenErr := http.ListenAndServe(httpPortString, r) + + // handle potential errors + if listenErr != nil { + log.Fatalln(listenErr.Error()) + } +} + // locationHandlerEndpoint handles requests to the /locations endpoint // This is used by the grafana worldmap plugin to find out where to draw the // fancy circles -- cgit 1.4.1