Skip to content

Commit c158d8a

Browse files
authored
Merge pull request #140 from haimait/feat/v3
Update cors.md
2 parents 8580acd + ef0e8f0 commit c158d8a

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

docs/faq/http/cors.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,55 @@ func main() {
148148
server.Start()
149149
}
150150
```
151+
152+
### 自定义复杂的跨域设置示例
153+
154+
示例代码如下:
155+
156+
```go
157+
package main
158+
159+
import (
160+
"flag"
161+
"fmt"
162+
"net/http"
163+
"os"
164+
165+
"github.com/zeromicro/go-zero/core/logx"
166+
"github.com/zeromicro/go-zero/core/conf"
167+
"github.com/zeromicro/go-zero/rest"
168+
"github.com/zeromicro/go-zero/rest/httpx"
169+
)
170+
171+
var configFile = flag.String("f", "etc/core-api-dev.yaml", "the config file")
172+
173+
func main() {
174+
flag.Parse()
175+
176+
var c config.Config
177+
conf.MustLoad(*configFile, &c)
178+
179+
# 需要通过的域名,这里可以写多个域名 或者可以写 * 全部通过
180+
domains := []string{"*","http://127.0.0.1", "https://go-zero.dev", "http://localhost"}
181+
server := rest.MustNewServer(
182+
c.RestConf,
183+
rest.WithCors(domains...),
184+
rest.WithCustomCors(func(header http.Header) {
185+
# 这里写允许通过的header key 不区分大小写
186+
header.Add("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token,Authorization,Token,X-Token,X-User-Id,OS,Platform, Version")
187+
header.Set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS,PATCH")
188+
header.Set("Access-Control-Expose-Headers", "Content-Length, Content-Type")
189+
}, nil, "*"),
190+
)
191+
192+
defer server.Stop()
193+
194+
ctx := svc.NewServiceContext(c)
195+
handler.RegisterHandlers(server, ctx)
196+
197+
198+
fmt.Printf("Starting admin_user-api server at %s:%d...\n", c.Host, c.Port)
199+
server.Start()
200+
}
201+
202+
```

docs/tutorials/api/parameter.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ slug: /docs/tutorials/api/parameter
1313

1414
在 api 描述语言中,我们可以通过在 tag 中来声明参数接收规则,目前 go-zero 支持的参数接收规则如下:
1515

16-
| <img width={100}/>接收规则 | 说明 | <img width={150}/>生效范围 | 示例 |
17-
| --- |-----------------------------------------------------------------------------------------------|----------------------------------| --- |
18-
| json | json 序列化 | 请求体&响应体 | \`json:"foo"\` |
19-
| path | 路由参数 | 请求体 | \`path:"id"\` |
20-
| form | post 请求的表单(支持 content-type 为 `form-data``x-www-form-urlencoded`) 参数请求接收标识,get 请求的 query 参数接收标识 | 请求体 | \`form:"name"\` |
21-
| header | http 请求体接收标识 | 请求体 |\`header:"Content-Length"\` |
16+
| <img width={100}/>接收规则 | 说明 | <img width={150}/>生效范围 | 接收tag示例 | 请求示例
17+
| --- |-----------------------------------------------------------------------------------------------|----------------------------------| --- | --- |
18+
| json | json 序列化 | 请求体&响应体 | json:"foo" | {"key":"vulue"} |
19+
| path | 路由参数 | 请求体 | path:"id" | /foo/:id |
20+
| form | post 请求的表单(支持 content-type 为 `form-data``x-www-form-urlencoded`) 参数请求接收标识,get 请求的 query 参数接收标识 | 请求体 | form:"name" | GET /search?key=vulue |
21+
| header | http 请求体接收标识 | 请求体 |header:"Content-Length" | origin: https://go-zero.dev
2222

2323
:::note 温馨提示
2424
go-zero 中不支持多 tag 来接收参数,即一个字段只能有一个 tag,如下写法可能会导致参数接收不到:

0 commit comments

Comments
 (0)