From 662a289b01a9e5da0af01d11a2f87e42909a308f Mon Sep 17 00:00:00 2001 From: Iliya Lyan <68940374+12ya@users.noreply.github.com> Date: Thu, 4 Sep 2025 22:06:09 +0900 Subject: [PATCH 1/2] Improve error handling in web server Added logging for server startup errors and ensured request body is closed after handling. This enhances resource management and provides better diagnostics if the server fails to start. --- lesson6/web_server_json_2.go | 5 +++++ 1 file changed, 5 insertions(+) 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) + } } From 4c393ff87b2082ef13ac33eac5285dc73ba9ab54 Mon Sep 17 00:00:00 2001 From: Iliya Lyan <68940374+12ya@users.noreply.github.com> Date: Thu, 4 Sep 2025 22:08:01 +0900 Subject: [PATCH 2/2] Improve server error handling and resource cleanup Added error handling for ListenAndServe in proxy and web server files to log fatal errors. Ensured HTTP request bodies are closed after use in client and server handlers to prevent resource leaks. --- lesson6/client_post.go | 1 + lesson6/proxy.go | 3 +++ lesson6/web_server_body.go | 5 +++++ lesson6/web_server_json.go | 6 ++++++ 4 files changed, 15 insertions(+) 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) + } }