Skip to content

Commit

Permalink
feat: add CORS docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Jul 30, 2024
1 parent d3c7746 commit 3ecf872
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 0 deletions.
150 changes: 150 additions & 0 deletions docs/faq/http/cors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# 跨域(CORS)

`go-zero` 针对跨域提供了三种用法:
- `rest.WithCors(origin ...string)`
- 设置允许跨域的域名
- `rest.WithCorsHeaders(headers ...string)`
- 设置允许跨域的 `header`
- `rest.WithCustomCors(middlewareFn func(header http.Header), notAllowedFn func(http.ResponseWriter),
origin ...string)`
- 设置复杂的跨域需求

## 如何自定义跨域 `header`

可以通过 `go-zero``rest.WithCorsHeaders(headers ...string)` 增加允许跨域的 `header`

示例代码如下:

```go
package main

import (
"fmt"
"net/http"

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

func main() {
var c rest.RestConf
conf.MustLoad("config.yaml", &c)

server := rest.MustNewServer(c, rest.WithCorsHeaders("UserHeader1", "UserHeader2"))
defer server.Stop()

server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/test",
Handler: func(w http.ResponseWriter, r *http.Request) {
logx.Info("请求进来了")
httpx.OkJsonCtx(r.Context(), w, "1")
},
},
},
)

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

> go-zero 版本:>= v1.7.1
## 如何自定义跨域 `domain`?

可以通过 `go-zero``rest.WithCors(origins ...string)` 设置允许跨域的域名。

示例代码如下:

```go
package main

import (
"fmt"
"net/http"

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

func main() {
var c rest.RestConf
conf.MustLoad("config.yaml", &c)

// 设置允许跨域的域名
server := rest.MustNewServer(c, rest.WithCors("example.com"))
defer server.Stop()

server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/test",
Handler: func(w http.ResponseWriter, r *http.Request) {
logx.Info("请求进来了")
httpx.OkJsonCtx(r.Context(), w, "1")
},
},
},
)

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

## 如何自定义复杂的跨域设置?

可以通过 `go-zero``rest.WithCustomCors` 设置复杂的跨域行为。

示例代码如下:

```go
package main

import (
"fmt"
"net/http"

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

func main() {
var c rest.RestConf
conf.MustLoad("config.yaml", &c)

server := rest.MustNewServer(c, rest.WithCustomCors(func(header http.Header) {
header.Set("Access-Control-Allow-Origin", "*")
header.Add("Access-Control-Allow-Headers", "UserHeader1, UserHeader2")
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()

server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/test",
Handler: func(w http.ResponseWriter, r *http.Request) {
logx.Info("请求进来了")
httpx.OkJsonCtx(r.Context(), w, "1")
},
},
},
)

fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
```
150 changes: 150 additions & 0 deletions i18n/en/docusaurus-plugin-content-docs/current/faq/http/cors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Cross-Origin Resource Sharing (CORS)

`go-zero` offers three ways to handle CORS:
- `rest.WithCors(origin ...string)`
- Sets the allowed origins for cross-origin requests.
- `rest.WithCorsHeaders(headers ...string)`
- Sets the allowed headers for cross-origin requests.
- `rest.WithCustomCors(middlewareFn func(header http.Header), notAllowedFn func(http.ResponseWriter),
origin ...string)`
- Configures complex CORS requirements.

## How to Customize CORS Headers?

You can add allowed headers for cross-origin requests using `go-zero`'s `rest.WithCorsHeaders(headers ...string)`.

Example code:

```go
package main

import (
"fmt"
"net/http"

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

func main() {
var c rest.RestConf
conf.MustLoad("config.yaml", &c)

server := rest.MustNewServer(c, rest.WithCorsHeaders("UserHeader1", "UserHeader2"))
defer server.Stop()

server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/test",
Handler: func(w http.ResponseWriter, r *http.Request) {
logx.Info("Request received")
httpx.OkJsonCtx(r.Context(), w, "1")
},
},
},
)

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

> go-zero version: >= v1.7.1
## How to Customize CORS Domains?

You can set allowed origins for cross-origin requests using go-zero’s rest.WithCors(origins ...string).

Example code:

```go
package main

import (
"fmt"
"net/http"

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

func main() {
var c rest.RestConf
conf.MustLoad("config.yaml", &c)

// Set allowed origins for cross-origin requests
server := rest.MustNewServer(c, rest.WithCors("example.com"))
defer server.Stop()

server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/test",
Handler: func(w http.ResponseWriter, r *http.Request) {
logx.Info("Request received")
httpx.OkJsonCtx(r.Context(), w, "1")
},
},
},
)

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

## How to Customize Complex CORS Settings?

You can configure complex CORS behavior using go-zero’s rest.WithCustomCors.

Example code:

```go
package main

import (
"fmt"
"net/http"

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

func main() {
var c rest.RestConf
conf.MustLoad("config.yaml", &c)

server := rest.MustNewServer(c, rest.WithCustomCors(func(header http.Header) {
header.Set("Access-Control-Allow-Origin", "*")
header.Add("Access-Control-Allow-Headers", "UserHeader1, UserHeader2")
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()

server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/test",
Handler: func(w http.ResponseWriter, r *http.Request) {
logx.Info("Request received")
httpx.OkJsonCtx(r.Context(), w, "1")
},
},
},
)

fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
```
1 change: 1 addition & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ module.exports = {
collapsed: false,
items: [
'faq/http/fileserver',
'faq/http/cors',
],
},
{
Expand Down

0 comments on commit 3ecf872

Please sign in to comment.