diff --git a/lesson6/client_post.go b/lesson6/client_post.go index e7728a8..532cea6 100644 --- a/lesson6/client_post.go +++ b/lesson6/client_post.go @@ -22,6 +22,7 @@ func main() { if err != nil { log.Fatal(err) } + defer httpResp.Body.Close() log.Printf("Status: %s", httpResp.Status) io.Copy(os.Stdout, httpResp.Body) } diff --git a/lesson6/proxy.go b/lesson6/proxy.go index 641006c..734871a 100644 --- a/lesson6/proxy.go +++ b/lesson6/proxy.go @@ -15,4 +15,7 @@ func main() { http.Handle("/", httputil.NewSingleHostReverseProxy(&target)) log.Printf("Proxy started") http.ListenAndServe("localhost:8081", nil) + if err := http.ListenAndServe("localhost:8081", nil); err != nil { + log.Fatal(err) + } } diff --git a/lesson6/web_server_body.go b/lesson6/web_server_body.go index 51cce9e..19eb481 100644 --- a/lesson6/web_server_body.go +++ b/lesson6/web_server_body.go @@ -3,14 +3,19 @@ package main import ( "fmt" "io" + "log" "net/http" "os" ) func main() { http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) { + defer req.Body.Close() fmt.Println("The body is:") io.Copy(os.Stdout, req.Body) }) http.ListenAndServe("localhost:8080", nil) + if err := http.ListenAndServe("localhost:8080", nil); err != nil { + log.Fatal(err) + } } diff --git a/lesson6/web_server_json.go b/lesson6/web_server_json.go index 7f47e3d..31d0dc2 100644 --- a/lesson6/web_server_json.go +++ b/lesson6/web_server_json.go @@ -3,18 +3,24 @@ package main import ( "encoding/json" "fmt" + "log" "net/http" ) func main() { http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) { + defer req.Body.Close() var params map[string]interface{} err := json.NewDecoder(req.Body).Decode(¶ms) if err != nil { fmt.Printf("Error parsing body: %s", err) + http.Error(w, fmt.Sprintf("Error parsing body: %s", err), http.StatusBadRequest) return } fmt.Printf("The body is: %#v", params) }) http.ListenAndServe("localhost:8080", nil) + if err := http.ListenAndServe("localhost:8080", nil); err != nil { + log.Fatal(err) + } } diff --git a/lesson6/web_server_json_2.go b/lesson6/web_server_json_2.go index 7fe1970..bc463f2 100644 --- a/lesson6/web_server_json_2.go +++ b/lesson6/web_server_json_2.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "log" "net/http" ) @@ -14,6 +15,7 @@ type Params struct { func main() { http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) { var params Params + defer req.Body.Close() err := json.NewDecoder(req.Body).Decode(¶ms) if err != nil { http.Error(w, fmt.Sprintf("Error parsing body: %s", err), http.StatusBadRequest) @@ -22,4 +24,7 @@ func main() { fmt.Fprintf(w, "Hello %s", params.Hello) }) http.ListenAndServe("localhost:8080", nil) + if err := http.ListenAndServe("localhost:8080", nil); err != nil { + log.Fatal(err) + } }