This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
87 lines (76 loc) · 2.16 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// For more details, read https://blog.jetbrains.com/go/2020/03/03/how-to-find-goroutines-during-debugging/
package main
import (
"context"
"log"
"net/http"
"time"
"github.com/dlsniper/debugger"
"github.com/gorilla/mux"
)
// Step 1. Run the code below using the debug mode
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Println("initializing the server...")
m := mux.NewRouter()
m.HandleFunc("/", debugger.Middleware(homeHandler, func(r *http.Request) []string {
return []string{
"request", "real",
"path", r.RequestURI,
}
}))
m.HandleFunc("/login", debugger.Middleware(loginHandler, func(r *http.Request) []string {
return []string{
"request", "real",
"path", r.RequestURI,
}
}))
m.HandleFunc("/logout", debugger.Middleware(logoutHandler, func(r *http.Request) []string {
return []string{
"request", "real",
"path", r.RequestURI,
}
}))
m.HandleFunc("/products", debugger.Middleware(productsHandler, func(r *http.Request) []string {
return []string{
"request", "real",
"path", r.RequestURI,
}
}))
m.HandleFunc("/product/{productID}", debugger.Middleware(productHandler, func(r *http.Request) []string {
return []string{
"request", "real",
"path", r.RequestURI,
}
}))
m.HandleFunc("/basket", debugger.Middleware(basketHandler, func(r *http.Request) []string {
return []string{
"request", "real",
"path", r.RequestURI,
}
}))
m.HandleFunc("/about", debugger.Middleware(aboutHandler, func(r *http.Request) []string {
return []string{
"request", "real",
"path", r.RequestURI,
}
}))
srv := http.Server{
Addr: "127.0.0.1:8080",
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 200 * 1000,
}
go fakeTraffic()
// Late binding of our shutdown path so that we don't call it from the traffic faker goroutine
m.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
_ = srv.Shutdown(context.Background())
})
srv.Handler = m
log.Println("starting the server...")
if err := srv.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}