Skip to content

Commit f3e191b

Browse files
committed
Updated README.md
1 parent 12ec806 commit f3e191b

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

README.md

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,6 @@ route.Prefix("/admin").Group(func(group *router.Route) {
150150
})
151151
```
152152

153-
## Middleware
154-
155-
Middleware provide a convenient mechanism for filtering HTTP requests entering your application. You only need to implement the `Middleware` interface.
156-
157-
```go
158-
route.Get("/foo", func(request *context.Request) *context.Response {
159-
return thinkgo.Text("Hello ThinkGo !")
160-
}).Middleware(func(request *context.Request, next router.Closure) interface{} {
161-
if _, err := request.Input("name"); err != nil {
162-
return thinkgo.Text("Invalid parameters")
163-
}
164-
return next(request)
165-
})
166-
```
167-
168153
#### Route Groups
169154

170155
Route groups allow you to share route attributes, such as middleware or prefix, across a large number of routes without needing to define those attributes on each individual route.
@@ -197,6 +182,51 @@ route.Prefix("/admin").Group(func(group *router.Route) {
197182
})
198183
```
199184

185+
## Middleware
186+
187+
Middleware provide a convenient mechanism for filtering HTTP requests entering your application. You only need to implement the `Middleware` interface.
188+
189+
```go
190+
route.Get("/foo", func(request *context.Request) *context.Response {
191+
return thinkgo.Text("Hello ThinkGo !")
192+
}).Middleware(func(request *context.Request, next router.Closure) interface{} {
193+
if _, err := request.Input("name"); err != nil {
194+
return thinkgo.Text("Invalid parameters")
195+
}
196+
return next(request)
197+
})
198+
```
199+
200+
#### Before Middleware
201+
202+
Whether a middleware runs before or after a request depends on the middleware itself. For example, the following middleware would perform some task `before` the request is handled by the application:
203+
204+
```go
205+
func(request *context.Request, next router.Closure) interface{} {
206+
207+
// Perform action
208+
// ...
209+
210+
return next(request)
211+
}
212+
```
213+
214+
#### After Middleware
215+
216+
However, this middleware would perform its task `after` the request is handled by the application:
217+
218+
```go
219+
func(request *context.Request, next router.Closure) interface{} {
220+
221+
response := next(request)
222+
223+
// Perform action
224+
// ...
225+
226+
return response
227+
}
228+
```
229+
200230
## Controller
201231

202232
#### Basic Controller

0 commit comments

Comments
 (0)