Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cors.md #140

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/faq/http/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,55 @@ func main() {
server.Start()
}
```

### 自定义复杂的跨域设置示例

示例代码如下:

```go
package main

import (
"flag"
"fmt"
"net/http"
"os"

"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
)

var configFile = flag.String("f", "etc/core-api-dev.yaml", "the config file")

func main() {
flag.Parse()

var c config.Config
conf.MustLoad(*configFile, &c)

# 需要通过的域名,这里可以写多个域名 或者可以写 * 全部通过
domains := []string{"*","http://127.0.0.1", "https://go-zero.dev", "http://localhost"}
server := rest.MustNewServer(
c.RestConf,
rest.WithCors(domains...),
rest.WithCustomCors(func(header http.Header) {
# 这里写允许通过的header key 不区分大小写
header.Add("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token,Authorization,Token,X-Token,X-User-Id,OS,Platform, Version")
header.Set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS,PATCH")
header.Set("Access-Control-Expose-Headers", "Content-Length, Content-Type")
}, nil, "*"),
)

defer server.Stop()

ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)


fmt.Printf("Starting admin_user-api server at %s:%d...\n", c.Host, c.Port)
server.Start()
}

```
12 changes: 6 additions & 6 deletions docs/tutorials/api/parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ slug: /docs/tutorials/api/parameter

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

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

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