-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
48 lines (42 loc) · 1.12 KB
/
server.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
package main
import (
"fmt"
"net/http"
"time"
)
func handler(w http.ResponseWriter, r *http.Request) {
// Check the network connectivity
if err := r.Body.(*http.Request).ParseForm(); err != nil {
http.Error(w, "Bad Request: Network connectivity issue", http.StatusBadRequest)
return
}
// Increase the server's read timeout
r.Body.(*http.Request).Header.Set("Timeout", "30")
r.Body.(*http.Request).Header.Set("Keep-Alive", "timeout=30")
buf := make([]byte, 1024)
_, err := r.Body.Read(buf)
if err != nil {
if err.Error() == "unexpected EOF" {
// Check the size of the request body
http.Error(w, "Bad Request: Request body is too short", http.StatusBadRequest)
return
}
fmt.Println("Error:", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
fmt.Fprint(w, "Hello, world!")
}
func main() {
http.HandleFunc("/", handler)
// Check the client implementation
srv := &http.Server{
Addr: ":8080",
ReadTimeout: 5 * time.Minute,
WriteTimeout: 10 * time.Minute,
IdleTimeout: 15 * time.Minute,
}
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}