Skip to content

Commit

Permalink
cors doc
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Aug 25, 2024
1 parent 1207a51 commit 78eb337
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 34 deletions.
10 changes: 4 additions & 6 deletions docs/book/features/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

CORS middleware can be used for [Cross-Origin Resource Sharing](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).

Modern browsers will block requests to a different domain unless that domain has CORS enabled. This middleware will add the appropriate headers to allow CORS requests from specified domains (You can allow multiple domains with [`AllowOrigin::list`] instance function).
Since the browser will send a `Method::OPTIONS` request, it is necessary to increase the processing of such requests and add middleware to `Service`.

_**Example**_

<CodeGroup>
<CodeGroupItem title="main.rs" active>

@[code rust](../../../codes/cors/src/main.rs)
@[code rust](../../../../codes/cors/src/main.rs)

</CodeGroupItem>
<CodeGroupItem title="Cargo.toml">

@[code toml](../../../codes/cors/Cargo.toml)
@[code toml](../../../../codes/cors/Cargo.toml)

</CodeGroupItem>
</CodeGroup>

[`AllowOrigin::list`]: https://docs.rs/salvo-cors/0.64.0/salvo_cors/struct.AllowOrigin.html#method.list
</CodeGroup>
4 changes: 1 addition & 3 deletions docs/es/book/features/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

El Intercambio de recursos entre Orígenes (CORS) puede ser usado por [Cross-Origin Resource Sharing](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).

Los navegadores modernos pueden bloquear las peticiones a diferentes dominios a menos que el dominio tenga habilitado el CORS. Éste middleware puede agregar la propiedad en las cabeceras para permitir peticiones desde dominios específico (Puedes permitir múltiples dominios con la instancia de la función [`AllowOrigin::list`]).
Dado que el navegador enviará una solicitud `Method::OPTIONS`, es necesario aumentar el procesamiento de dichas solicitudes y agregar middleware a `Service`.

_**Ejemplo**_

Expand All @@ -18,5 +18,3 @@ _**Ejemplo**_

</CodeGroupItem>
</CodeGroup>

[`AllowOrigin::list`]: https://docs.rs/salvo-cors/0.64.0/salvo_cors/struct.AllowOrigin.html#method.list
5 changes: 2 additions & 3 deletions docs/zh-hans/book/features/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

CORS 中间件可以用于 [跨源资源共享](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS).

由于浏览器会发 `Method::OPTIONS` 的请求, 所以需要增加对此类请求的处理, 需要吧中间件添加到 `Service` 上.

_**示例代码**_
由于浏览器会发 `Method::OPTIONS` 的请求, 所以需要增加对此类请求的处理, 需要把中间件添加到 `Service` 上.

_**示例代码**_

<CodeGroup>
<CodeGroupItem title="main.rs" active>
Expand Down
24 changes: 2 additions & 22 deletions docs/zh-hant/book/features/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,9 @@

CORS 中間件可以用於 [跨源資源共享](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS).

由於瀏覽器會發 `Method::OPTIONS` 的請求, 所以需要增加對此類請求的處理, 有兩種方式可以處理 `OPTIONS` 的請求.

- 可以對根 `Router` 添加 `empty_handler`

```rust
Router::with_hoop(cors).get(hello).options(handler::empty());
```

- 可以在 Catcher 上添加 `cors` 的 Handler:

```rust
let cors = Cors::new()
.allow_origin(["http://127.0.0.1:5800", "http://localhost:5800"])
.allow_methods(vec![Method::GET, Method::POST, Method::DELETE])
.allow_headers("authorization")
.into_handler();
let acceptor = TcpListener::new("0.0.0.0:5600").bind().await;
let service = Service::new(router).catcher(Catcher::default().hoop(cors));
Server::new(acceptor).serve(service).await;
```

_**示例代碼**_
由於瀏覽器會發 `Method::OPTIONS` 的請求, 所以需要增加對此類請求的處理, 需要把中間件添加到 `Service` 上.

_**示例代码**_

<CodeGroup>
<CodeGroupItem title="main.rs" active>
Expand Down

0 comments on commit 78eb337

Please sign in to comment.