-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
55 lines (48 loc) · 1.15 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
53
54
55
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type request struct {
Method string `json:"method"`
URL string `json:"url"`
Headers map[string][]string `json:"headers"`
Body string `json:"body"`
RemoteAddr string `json:"remote_address"`
}
func datafy(r *http.Request) *request {
headers := make(map[string][]string)
for name, value := range r.Header {
headers[name] = value
}
bs, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("error reading request body: %v\n", err)
bs = []byte("<error reading body>")
}
return &request{
Method: r.Method,
URL: r.URL.String(),
Headers: headers,
Body: string(bs),
RemoteAddr: r.RemoteAddr,
}
}
func echoRequest(w http.ResponseWriter, r *http.Request) {
request := datafy(r)
encoder := json.NewEncoder(w)
err := encoder.Encode(&request)
if err != nil {
log.Printf("error echoing request: %v\n", err)
}
}
func main() {
http.HandleFunc("/", echoRequest)
log.Print("started server at :8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}