package libme import ( "fmt" "io/ioutil" "net/http" "os" "strings" "go.wit.com/log" ) // remove '?' part and trailing '/' func cleanURL(url string) string { url = "/" + strings.Trim(url, "/") return url } func okHandler(w http.ResponseWriter, r *http.Request) { var route string log.Info("Got URL Path: ", r.URL.Path) log.Info("Got URL Query:", r.URL.Query().Get("start")) route = cleanURL(r.URL.Path) msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte if err != nil { fmt.Fprintln(w, "ReadAll() error =", err) return } log.Info("Got URL msg:", string(msg)) if route == "/" { fmt.Fprintln(w, "OK") return } if route == "/me" { j, err := dumpJsonClient(r) if err != nil { fmt.Fprintln(w, "json parse error") return } fmt.Fprintln(w, j) return } if route == "/favicon.ico" { w.Header().Set("Content-Type", "image/png") } if route == "/kill" { log.Warn("KILLED") fmt.Fprintln(w, "KILLED") os.Exit(-1) return } log.Warn("BAD URL =", route) fmt.Fprintln(w, "BAD URL =", route) } // starts and sits waiting for HTTP requests func StartMe(port int) { http.HandleFunc("/", okHandler) p := fmt.Sprintf(":%d", port) log.Println("Running on port", p) err := http.ListenAndServe(p, nil) if err != nil { log.Println("Error starting server:", err) } }