-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathserver.go
46 lines (40 loc) · 858 Bytes
/
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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
func handleUserAPI(w http.ResponseWriter, r *http.Request) {
log.Println("I started processing the request")
defer r.Body.Close()
data, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading body: %v\n", err)
http.Error(
w, "Error reading body",
http.StatusInternalServerError,
)
return
}
log.Println(string(data))
fmt.Fprintf(w, "Hello world!")
log.Println("I finished processing the request")
}
func main() {
listenAddr := os.Getenv("LISTEN_ADDR")
if len(listenAddr) == 0 {
listenAddr = ":8080"
}
mux := http.NewServeMux()
mux.HandleFunc("/api/users/", handleUserAPI)
s := http.Server{
Addr: listenAddr,
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
log.Fatal(s.ListenAndServe())
}