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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"text/template"
"github.com/gorilla/mux"
)
var (
port *int
metrics map[string]float64
)
// setupHTTPServer returns an http server
func setupHTTPServer() http.Server {
r := mux.NewRouter()
r.HandleFunc("/", indexHandler).Methods("GET", "POST")
r.HandleFunc("/metrics", metricsHandler).Methods("GET", "POST")
return http.Server{
Addr: fmt.Sprintf("0.0.0.0:%d", *port),
Handler: r,
}
}
// registerHTTPFlags registers the flags needed for the http server
func registerHTTPFlags() {
port = flag.Int("p", 8084, "The port for the HTTP server")
}
// indexHandler handles requests to the "/" endpoint
func indexHandler(w http.ResponseWriter, r *http.Request) {
log.Println("index accessed")
switch r.Method {
case "GET":
log.Println("get")
metricsStruct := Metrics{
Metrics: metrics,
}
t := template.New("")
t, err := t.ParseFiles("./hosted/index.html")
if err != nil {
}
t.ExecuteTemplate(w, "index", metricsStruct)
//readFileToResponse(w, "index.html", metricsStruct)
case "POST":
log.Println("post")
// parse the post form and add the metric to the metrics list
errParseForm := r.ParseForm()
if errParseForm != nil {
panic(errParseForm)
}
// parse the parameters
key := r.Form.Get("key")
value, parseFloatErr := strconv.ParseFloat(r.Form.Get("value"), 64)
if parseFloatErr != nil {
log.Printf("Error parsing %v", value)
}
log.Printf("key: %v", key)
log.Printf("value: %v", value)
// if the metrics var does not exist yet, create it
if metrics == nil {
metrics = map[string]float64{}
}
// assign the value to the key
metrics[key] = value
http.Redirect(w, r, "/", http.StatusSeeOther)
default:
log.Println("Error, the handler was neither accessed using a GET or a POST request!")
}
}
// metricsHandler handles requests to the /metrics endpoint
func metricsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
// handle an empty metrics map
if len(metrics) == 0 {
returnString := "Add metrics by making a post request to this endpoint\n" +
"curl -X POST --data \"key=asd&value=asd\" <url>:<port>/metrics"
_, _ = fmt.Fprintf(w, returnString)
// else, return all the metrics
} else {
for key, value := range metrics {
_, _ = fmt.Fprintf(w, "%s %f\n", key, value)
}
}
} else if r.Method == "POST" {
// parse the POST form
errParseForm := r.ParseForm()
if errParseForm != nil {
panic(errParseForm)
}
// parse the parameters
key := r.Form.Get("key")
value, parseFloatErr := strconv.ParseFloat(r.Form.Get("value"), 64)
if parseFloatErr != nil {
log.Printf("Error parsing %v", value)
}
// if the metrics var does not exist yet, create it
if metrics == nil {
metrics = map[string]float64{}
}
// assign the value to the key
metrics[key] = value
_, _ = fmt.Fprintf(w, "added the metrics")
} else {
log.Println("Error, the handler was neither accessed using a GET or a POST request!")
}
}
func readFileToResponse(w http.ResponseWriter, path string) {
requestedFile := strings.Replace(path, "..", "", -1)
contents, readError := ioutil.ReadFile(fmt.Sprintf("hosted/%s", requestedFile))
if readError != nil {
w.Write([]byte(fmt.Sprintf("unable to read %s", requestedFile)))
} else {
w.Write([]byte(contents))
}
}
|