-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (42 loc) · 1.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
func main() {
log.Println("Starting application")
port := flag.Int("port", 8080, "port to listen on")
flag.Parse()
http.HandleFunc("/", printRequest)
listenAddr := fmt.Sprintf("%s:%d", "", *port)
if err := http.ListenAndServe(listenAddr, nil); err != nil {
panic(err)
}
}
func printRequest(w http.ResponseWriter, r *http.Request) {
var output []string // Add the request string
output = append(output, fmt.Sprintf("%s %s %s", r.Method, r.URL, r.Proto))
output = append(output, fmt.Sprintf("Host: %v", r.Host))
for name, headers := range r.Header {
name = strings.ToLower(name)
for _, h := range headers {
output = append(output, fmt.Sprintf("%v: %v", name, h))
}
}
// If this is a POST, add post data
if r.Method == "POST" {
output = append(output, "\n")
if body, err := ioutil.ReadAll(r.Body); err != nil {
output = append(output, err.Error())
} else {
output = append(output, string(body))
}
}
result := strings.Join(output, "\n") + "\n"
log.Println(fmt.Sprintf("Received request:\n%s\n", result))
fmt.Fprintf(w, result)
}