diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/http.go | 121 |
1 files changed, 118 insertions, 3 deletions
diff --git a/src/http.go b/src/http.go index 7c0db12..397d748 100644 --- a/src/http.go +++ b/src/http.go @@ -3,20 +3,27 @@ package main import ( "flag" "fmt" + "io/ioutil" + "log" "net/http" + "strconv" + "strings" + "text/template" "github.com/gorilla/mux" ) var ( - port *int + port *int + metrics map[string]float64 ) // setupHTTPServer returns an http server func setupHTTPServer() http.Server { r := mux.NewRouter() - r.HandleFunc("/", indexHandler).Methods("GET") + 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), @@ -31,5 +38,113 @@ func registerHTTPFlags() { // indexHandler handles requests to the "/" endpoint func indexHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "%s", "asd") + 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)) + } } |