1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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])
}
}
}
|