-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
42 lines (35 loc) · 786 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
func echoHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Unable to read request body", http.StatusInternalServerError)
return
}
response := map[string]interface{}{
"method": r.Method,
"url": r.URL.String(),
"header": r.Header,
"body": string(body),
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func main() {
port := os.Getenv("HTTP_ECHO_PORT")
if port == "" {
port = "8080"
}
port = fmt.Sprintf(":%s", port)
http.HandleFunc("/", echoHandler)
if err := http.ListenAndServe(port, nil); err != nil {
log.Fatal("Failed to start server:", err)
}
}