Skip to content

Commit c75a558

Browse files
authored
docs: modify web filter docs (#16)
1 parent 26fe3f7 commit c75a558

File tree

2 files changed

+123
-4
lines changed

2 files changed

+123
-4
lines changed

docs/en-US/v2.3.x/web/filter/README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,82 @@ func InsertFilter(pattern string, pos int, filter FilterFunc, opts ...FilterOpt)
5555
```
5656

5757
- `pattern`: string or regex to match against router rules. Use `/*` to match all.
58+
5859
- `pos`: the place to execute the Filter. There are five fixed parameters representing different execution processes.
5960
- web.BeforeStatic: Before finding the static file.
6061
- web.BeforeRouter: Before finding router.
6162
- web.BeforeExec: After finding router and before executing the matched Controller.
6263
- web.AfterExec: After executing Controller.
6364
- web.FinishRouter: After finishing router.
65+
6466
- `filter`: filter function type FilterFunc func(*context.Context)
67+
6568
- `opts`:
66-
- `web.WithReturnOnOutput`: sets the value of `returnOnOutput` (default `true`), if there is already output before this filter is performed, whether to not continue to execute this filter, the default setting is that if there is already output before (parameter `true`), then this filter will not be executed.
69+
70+
- `web.WithReturnOnOutput`: sets the value of `returnOnOutput` (default `true`), if there is already output before this filter is performed, whether to not continue to execute this filter, the default setting is that if there is already output before (parameter `true`), then this filter will not be executed.
71+
72+
(**Note: When using the AfterExec or FinishRouter positions, you must set WithReturnOnOutput to false, otherwise the filter will not take effect. See the example below for details.**);
73+
6774
- `web.WithResetParams`: Whether to reset the `filter` parameter, the default is `false`, because in case of conflict between the `pattern` of the `filter` and the `pattern` of the route itself, you can reset the `filter` parameter, so as to ensure that the correct parameter is obtained in the subsequent logic, for example, if the filter of `/api/*` is set, and the router of `/api/docs/*` is set at the same time, then when the router of `/api/docs/*` is executed, then the correct parameter is obtained in the subsequent logic. For example, if you set the filter of `/api/*` and also set the router of `/api/docs/*`, then when you access `/api/docs/swagger/abc.js`, set `:splat` parameter to `docs/swagger/abc.js` when executing `filter`, but if the option is `false`, it will keep `docs/swagger/abc.js` when executing the routing logic, and reset the `:splat` parameter if `true` is set.
75+
6876
- `web.WithCaseSensitive`: case sensitive;
6977

7078
If it is not clear how to use these options, the best way is to write a few tests yourself to experiment with their effects.
7179

80+
For example, here is an example of the InsertFilter method in the current beego project:
81+
82+
```go
83+
// ExampleInsertFilter is an example of how to use InsertFilter
84+
func ExampleInsertFilter() {
85+
86+
app := NewHttpServerWithCfg(newBConfig())
87+
app.Cfg.CopyRequestBody = true
88+
path := "/api/hello"
89+
app.Get(path, func(ctx *context.Context) {
90+
s := "hello world"
91+
fmt.Println(s)
92+
_ = ctx.Resp(s)
93+
})
94+
95+
app.InsertFilter("*", BeforeStatic, func(ctx *context.Context) {
96+
fmt.Println("BeforeStatic filter process")
97+
})
98+
99+
app.InsertFilter("*", BeforeRouter, func(ctx *context.Context) {
100+
fmt.Println("BeforeRouter filter process")
101+
})
102+
103+
app.InsertFilter("*", BeforeExec, func(ctx *context.Context) {
104+
fmt.Println("BeforeExec filter process")
105+
})
106+
107+
// need to set the WithReturnOnOutput false
108+
app.InsertFilter("*", AfterExec, func(ctx *context.Context) {
109+
fmt.Println("AfterExec filter process")
110+
}, WithReturnOnOutput(false))
111+
112+
// need to set the WithReturnOnOutput false
113+
app.InsertFilter("*", FinishRouter, func(ctx *context.Context) {
114+
fmt.Println("FinishRouter filter process")
115+
}, WithReturnOnOutput(false))
116+
117+
reader := strings.NewReader("")
118+
req := httptest.NewRequest("GET", path, reader)
119+
req.Header.Set("Accept", "*/*")
120+
121+
w := httptest.NewRecorder()
122+
app.Handlers.ServeHTTP(w, req)
123+
124+
// Output:
125+
// BeforeStatic filter process
126+
// BeforeRouter filter process
127+
// BeforeExec filter process
128+
// hello world
129+
// AfterExec filter process
130+
// FinishRouter filter process
131+
}
132+
```
133+
72134
Here is an example to authenticate if the user is logged in for all requests:
73135

74136
```go

docs/zh/v2.3.x/web/filter/README.md

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,71 @@ func InsertFilter(pattern string, pos int, filter FilterFunc, opts ...FilterOpt)
7676

7777
`opts` 对应三个选项:
7878

79-
- `web.WithReturnOnOutput`: 设置 `returnOnOutput` 的值(默认`true`), 如果在进行到此过滤之前已经有输出,是否不再继续执行此过滤器,默认设置为如果前面已有输出(参数为`true`),则不再执行此过滤器;
80-
-
79+
- `web.WithReturnOnOutput`: 设置 `returnOnOutput` 的值(默认`true`), 如果在进行到此过滤之前已经有输出,是否不再继续执行此过滤器,默认设置为如果前面已有输出(参数为`true`),则不再执行此过滤器。
80+
81+
(**特别注意:在使用 AfterExec、FinishRouter 这2个pos位置路由时,需要设置WithReturnOnOutput为false,不然不会生效,详见下面案例**);
82+
8183
- `web.WithResetParams`: 是否重置`filter`的参数,默认是`false`,因为在`filter``pattern`和本身的路由的`pattern`冲突的时候,可以把`filter`的参数重置,这样可以保证在后续的逻辑中获取到正确的参数,例如设置了`/api/*` 的 filter,同时又设置了 `/api/docs/*` 的 router,那么在访问 `/api/docs/swagger/abc.js` 的时候,在执行`filter`的时候设置 `:splat` 参数为 `docs/swagger/abc.js`,但是如果该选项为 `false`,就会在执行路由逻辑的时候保持 `docs/swagger/abc.js`,如果设置了`true`,就会重置 `:splat` 参数;
84+
8285
- `web.WithCaseSensitive`: 是否大小写敏感;
8386

8487
如果不清楚如何使用这些选项,最好的方法是自己写几个测试来试验一下它们的效果。
8588

86-
我们在看一个验证登录态的例子。该例子是假设启用了 Beego 的`session`模块:
89+
比如,目前beego项目里面的InsertFilter方法的Example:
90+
91+
```go
92+
// ExampleInsertFilter is an example of how to use InsertFilter
93+
func ExampleInsertFilter() {
94+
95+
app := NewHttpServerWithCfg(newBConfig())
96+
app.Cfg.CopyRequestBody = true
97+
path := "/api/hello"
98+
app.Get(path, func(ctx *context.Context) {
99+
s := "hello world"
100+
fmt.Println(s)
101+
_ = ctx.Resp(s)
102+
})
103+
104+
app.InsertFilter("*", BeforeStatic, func(ctx *context.Context) {
105+
fmt.Println("BeforeStatic filter process")
106+
})
107+
108+
app.InsertFilter("*", BeforeRouter, func(ctx *context.Context) {
109+
fmt.Println("BeforeRouter filter process")
110+
})
111+
112+
app.InsertFilter("*", BeforeExec, func(ctx *context.Context) {
113+
fmt.Println("BeforeExec filter process")
114+
})
115+
116+
// need to set the WithReturnOnOutput false
117+
app.InsertFilter("*", AfterExec, func(ctx *context.Context) {
118+
fmt.Println("AfterExec filter process")
119+
}, WithReturnOnOutput(false))
120+
121+
// need to set the WithReturnOnOutput false
122+
app.InsertFilter("*", FinishRouter, func(ctx *context.Context) {
123+
fmt.Println("FinishRouter filter process")
124+
}, WithReturnOnOutput(false))
125+
126+
reader := strings.NewReader("")
127+
req := httptest.NewRequest("GET", path, reader)
128+
req.Header.Set("Accept", "*/*")
129+
130+
w := httptest.NewRecorder()
131+
app.Handlers.ServeHTTP(w, req)
132+
133+
// Output:
134+
// BeforeStatic filter process
135+
// BeforeRouter filter process
136+
// BeforeExec filter process
137+
// hello world
138+
// AfterExec filter process
139+
// FinishRouter filter process
140+
}
141+
```
142+
143+
我们再看一个验证登录态的例子。该例子是假设启用了 Beego 的`session`模块:
87144

88145
```go
89146
var FilterUser = func(ctx *context.Context) {

0 commit comments

Comments
 (0)