summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlexander Karl <akarl@darknebu.la>2019-02-13 19:31:34 +0100
committerAlexander Karl <akarl@darknebu.la>2019-02-13 19:31:34 +0100
commit9e24a8f83036cd36838a0557f130608233e11715 (patch)
tree55906967869c3913b0fbc798147fa3b56d609381
Init commit - welcome http-grab-url HEAD master
-rw-r--r--main.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..b1a6edb
--- /dev/null
+++ b/main.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+    "fmt"
+    "net/http"
+    "log"
+)
+
+var(
+    metrics_num_urls int
+)
+
+func main() {
+    log.Println("Starting HTTP listener")
+
+    http.HandleFunc("/", httpHandler)
+    http.HandleFunc("/metrics", metricsHandler)
+    listenErr := http.ListenAndServe(":80", nil) // set listen port
+    if listenErr != nil {
+        log.Fatalln(listenErr.Error())
+    }
+}
+
+// Handling incoming HTTP connections
+func httpHandler(w http.ResponseWriter, r *http.Request) {
+    metrics_num_urls++
+    log.Printf("(%s) %s %s", r.Method, r.RemoteAddr, r.URL.Path)
+}
+
+// Handle HTTP /metrics requests
+func metricsHandler(w http.ResponseWriter, req *http.Request) {
+    fmt.Fprintf(w, "num_urls %d\n", metrics_num_urls)
+}