about summary refs log tree commit diff
path: root/src/http.go
blob: a08845e5b4c10f137e31c8452455d28a42897674 (plain)
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
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 userFlags == nil {
		userFlags = make(map[string]int)
	}

	fmt.Fprintf(w, "%s\n", "scoreboard_up 1")

	for _, user := range users {
		if user != "" {
			fmt.Fprintf(w, "challengesSolves{name=\"%s\"} %d\n", user, userFlags[user])
		}
	}
}