package main import ( "fmt" "net/http" "github.com/gorilla/mux" ) // setupHTTPServer defines a new http server func setupHTTPServer() http.Server { r := mux.NewRouter() r.HandleFunc("/", indexHandler) r.HandleFunc("/metrics", metricsHandler) return http.Server{ Addr: fmt.Sprintf("0.0.0.0:%d", *port), Handler: r, } } // indexHandler handles the "/" endpoint func indexHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s", "You can find the metrics by accessing the /metrics endpoint!\n") } // metricsHandler handles the "/metrics" endpoint func metricsHandler(w http.ResponseWriter, r *http.Request) { if userScore == nil { userScore = make(map[string]int) } fmt.Fprintf(w, "%s\n", "scoreboard_up 1") fmt.Fprintf(w, "%s\n", "version 1") for _, user := range users { if user != "" { fmt.Fprintf(w, "score{name=\"%s\"} %d\n", user, userScore[user]) } } }