docgen_test.go
6.11 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package docgen_test
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/meiqia/chi"
"github.com/meiqia/chi/docgen"
)
// RequestID comment goes here.
func RequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "requestID", "1")
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func hubIndexHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s reqid:%s session:%s",
chi.URLParam(r, "hubID"), ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
}
// Generate docs for the MuxBig from chi/mux_test.go
func TestMuxBig(t *testing.T) {
var r, sr1, sr2, sr3, sr4, sr5, sr6 *chi.Mux
r = chi.NewRouter()
r.Use(RequestID)
// Some inline middleware, 1
// We just love Go's ast tools
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
})
r.Group(func(r chi.Router) {
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "session.user", "anonymous")
next.ServeHTTP(w, r.WithContext(ctx))
})
})
r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("fav"))
})
r.Get("/hubs/:hubID/view", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/view reqid:%s session:%s", chi.URLParam(r, "hubID"),
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/hubs/:hubID/view/*", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/view/%s reqid:%s session:%s", chi.URLParamFromCtx(ctx, "hubID"),
chi.URLParam(r, "*"), ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
})
r.Group(func(r chi.Router) {
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "session.user", "elvis")
next.ServeHTTP(w, r.WithContext(ctx))
})
})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/ reqid:%s session:%s", ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/suggestions", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/suggestions reqid:%s session:%s", ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/woot/:wootID/*", func(w http.ResponseWriter, r *http.Request) {
s := fmt.Sprintf("/woot/%s/%s", chi.URLParam(r, "wootID"), chi.URLParam(r, "*"))
w.Write([]byte(s))
})
r.Route("/hubs", func(r chi.Router) {
sr1 = r.(*chi.Mux)
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
})
r.Route("/:hubID", func(r chi.Router) {
sr2 = r.(*chi.Mux)
r.Get("/", hubIndexHandler)
r.Get("/touch", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/touch reqid:%s session:%s", chi.URLParam(r, "hubID"),
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
sr3 = chi.NewRouter()
sr3.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/webhooks reqid:%s session:%s", chi.URLParam(r, "hubID"),
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
sr3.Route("/:webhookID", func(r chi.Router) {
sr4 = r.(*chi.Mux)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/webhooks/%s reqid:%s session:%s", chi.URLParam(r, "hubID"),
chi.URLParam(r, "webhookID"), ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
})
// TODO: /webooks is not coming up as a subrouter here...
// we kind of want to wrap a Router... ?
// perhaps add .Router() to the middleware inline thing..
// and use that always.. or, can detect in that method..
r.Mount("/webhooks", chi.Chain(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "hook", true)))
})
}).Handler(sr3))
// HMMMM.. only let Mount() for just a Router..?
// r.Mount("/webhooks", Use(...).Router(sr3))
// ... could this work even....?
// HMMMMMMMMMMMMMMMMMMMMMMMM...
// even if Mount() were to record all subhandlers mounted, we still couldn't get at the
// routes
r.Route("/posts", func(r chi.Router) {
sr5 = r.(*chi.Mux)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/posts reqid:%s session:%s", chi.URLParam(r, "hubID"),
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
})
})
})
r.Route("/folders/", func(r chi.Router) {
sr6 = r.(*chi.Mux)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/folders/ reqid:%s session:%s",
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/public", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/folders/public reqid:%s session:%s",
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/in", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}).ServeHTTP)
r.With(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "search", true)))
})
}).Get("/search", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("searching.."))
})
})
})
fmt.Println(docgen.JSONRoutesDoc(r))
// docgen.PrintRoutes(r)
}