forked from go-chi/chi
/
main.go
123 lines (98 loc) · 3.09 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"net/http"
"github.com/pressly/chi"
"github.com/pressly/chi/middleware"
"golang.org/x/net/context"
)
func main() {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(func(h chi.Handler) chi.Handler {
return chi.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, "example", true)
h.ServeHTTPC(ctx, w, r)
})
})
r.Get("/", apiIndex)
r.Mount("/accounts", accountsRouter())
http.ListenAndServe(":3333", r)
}
func accountsRouter() chi.Router { // or http.Handler
r := chi.NewRouter()
r.Use(sup1)
r.Get("/", listAccounts)
r.Post("/", createAccount)
r.Get("/hi", hiAccounts)
r.Group(func(r chi.Router) {
r.Use(sup2)
r.Get("/hi2", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
v := ctx.Value("sup2").(string)
w.Write([]byte(fmt.Sprintf("hi2 - '%s'", v)))
})
r.Get("/ahh", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
v := ctx.Value("sup2").(string)
w.Write([]byte(fmt.Sprintf("ahh - '%s'", v)))
})
r.Get("/fail", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
panic("no..")
})
})
r.Route("/:accountID", func(r chi.Router) {
r.Use(accountCtx)
r.Get("/", getAccount)
r.Put("/", updateAccount)
r.Get("/*", other)
})
return r
}
func sup1(next chi.Handler) chi.Handler {
hfn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, "sup1", "sup1")
next.ServeHTTPC(ctx, w, r)
}
return chi.HandlerFunc(hfn)
}
func sup2(next chi.Handler) chi.Handler {
hfn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, "sup2", "sup2")
next.ServeHTTPC(ctx, w, r)
}
return chi.HandlerFunc(hfn)
}
func accountCtx(h chi.Handler) chi.Handler {
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, "account", "account 123")
h.ServeHTTPC(ctx, w, r)
}
return chi.HandlerFunc(handler)
}
func apiIndex(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("root"))
}
func listAccounts(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("list accounts"))
}
func hiAccounts(ctx context.Context, w http.ResponseWriter, r *http.Request) {
sup1 := ctx.Value("sup1").(string)
w.Write([]byte(fmt.Sprintf("hi accounts %v", sup1)))
}
func createAccount(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("create account"))
}
func getAccount(ctx context.Context, w http.ResponseWriter, r *http.Request) {
accountID := chi.URLParams(ctx)["accountID"]
account := ctx.Value("account").(string)
w.Write([]byte(fmt.Sprintf("get account id:%s details:%s", accountID, account)))
}
func updateAccount(ctx context.Context, w http.ResponseWriter, r *http.Request) {
account := ctx.Value("account").(string)
w.Write([]byte(fmt.Sprintf("update account:%s", account)))
}
func other(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("catch all.."))
}