1: package main
2:
3: import (
4: "fmt"
5: "io/ioutil"
6: "log"
7: "net/http"
8: )
9:
10: func helloWorld(w http.ResponseWriter, r *http.Request) {
11: if r.URL.Path != "/" {
12: http.NotFound(w, r)
13: return
14: }
15: switch r.Method {
16: case "GET":
17: for k, v := range r.URL.Query() {
18: fmt.Printf("%s: %s\n", k, v)
19: }
20: w.Write([]byte("Received a GET request\n"))
21: case "POST":
22: reqBody, err := ioutil.ReadAll(r.Body)
23: if err != nil {
24: log.Fatal(err)
25: }
26:
27: fmt.Printf("%s\n", reqBody)
28: w.Write([]byte("Received a POST request\n"))
29: default:
30: w.WriteHeader(http.StatusNotImplemented)
31: w.Write([]byte(http.StatusText(http.StatusNotImplemented)))
32: }
33:
34: }
35:
36: func main() {
37: http.HandleFunc("/", helloWorld)
38: http.ListenAndServe(":8000", nil)
39: }