-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
36 lines (27 loc) · 783 Bytes
/
main.go
File metadata and controls
36 lines (27 loc) · 783 Bytes
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
package main
import (
"log"
"net/http"
)
func logger(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("REQ", r.Method, r.URL.Path)
handler.ServeHTTP(w, r)
})
}
func main() {
mux := http.NewServeMux()
muxWithLogger := logger(mux)
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World !!"))
})
mux.HandleFunc("GET /posts/{postId}", func(w http.ResponseWriter, r *http.Request) {
postId := r.PathValue("postId")
log.Println("POST ID", postId)
w.Write([]byte("success"))
})
log.Println("server stared on port 8080")
if err := http.ListenAndServe(":8080", muxWithLogger); err != nil {
panic(err)
}
}