about summary refs log tree commit diff
path: root/src/http.go
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2020-01-27 20:13:15 +0100
committerEmile <hanemile@protonmail.com>2020-01-27 20:13:15 +0100
commit651f15d9b8bde1b024f97a42e92634afdc1eeda7 (patch)
treec6dfb94751084d720559c04563b35d31ae9f074a /src/http.go
parent7be2a6d747a767d016b976de60ce2bb21d3a5203 (diff)
moved the source into an own folder
Diffstat (limited to 'src/http.go')
-rw-r--r--src/http.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/http.go b/src/http.go
new file mode 100644
index 0000000..7aac0c7
--- /dev/null
+++ b/src/http.go
@@ -0,0 +1,52 @@
+package main
+
+// locationHandlerEndpoint handles requests to the /locations endpoint
+// This is used by the grafana worldmap plugin to find out where to draw the
+// fancy circles
+func locationHandlerEndpoint(w http.ResponseWriter, r *http.Request) {
+
+	// set some headers
+	w.Header().Set("Content-Type", "application/json")
+	w.Header().Set("Access-Control-Allow-Origin", "https://grafana.nbg1.emile.space")
+
+	// start building json (yes, this is not a nice implementation, PRs welcome!)
+	fmt.Fprintf(w, "%s", "[")
+
+	var i int = 0
+	for _, v := range cities {
+
+		// print the "json" object containing the metrics needed
+		fmt.Fprintf(w, "{")
+		fmt.Fprintf(w, "\"key\": \"%s\",", v.key)
+		fmt.Fprintf(w, "\"latitude\": %f,", v.latitude)
+		fmt.Fprintf(w, "\"longitude\": %f,", v.longitude)
+		fmt.Fprintf(w, "\"name\": \"%s\"", v.name)
+
+		// close the object (this handles the trailing comma problem)
+		if i == len(cities) - 1 {
+			fmt.Fprintf(w, "}")
+		} else {
+			fmt.Fprintf(w, "},")
+		}
+		i++
+	}
+	fmt.Fprintf(w, "%s", "]")
+}
+
+// indexHandler handles the request to the / endpoint
+// It simply returns a link to the /metrics page
+func indexHandler(w http.ResponseWriter, req *http.Request) {
+	_, _ = fmt.Fprintf(w, "<a href='/metrics'>metrics</a>")
+}
+
+// Handle HTTP requests to the /metrics endpoint
+func metricsHandler(w http.ResponseWriter, req *http.Request) {
+
+	// return the overall amount of passwords catched
+	fmt.Fprintf(w, "num_passwords %d\n", metrics_num_passwords)
+
+	// return the amount of passwords catched from a given city
+	for k, v := range metrics_city_num {
+		fmt.Fprintf(w, "a_metric{city=\"%s\"} %d\n", strings.ToLower(k), v)
+	}
+}
\ No newline at end of file