-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented CORS header handler middleware (#262)
* feat: Add cors middleware * test: Add cors test code * feat: export CorsMiddleware * feat: Add cors example * $cargo fmt * doc: move example/cors.rs * doc: Add README * doc: Add doc comment * fix example doc comment * fix: Changed type to option * feat: Add credentials header at preflight request * fix: Attach an expose header when requesting preflight * $cargo fmt * fix: remove debug pring * refactor: move build preflight response * fix: Remove echo_back option * fix: fix type OPTION -> OPTIONS * refactor: tide-cors to optional dependence * $cargo fmt * refactor: imple arg to generic argument * fix typo * refactor: using 'match'
- Loading branch information
1 parent
2504828
commit 1fb4ab3
Showing
7 changed files
with
430 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "tide-cors" | ||
version = "0.2.0" | ||
authors = [ | ||
"Tide Developers", | ||
] | ||
description = "Cors middleware and extensions for Tide" | ||
documentation = "https://docs.rs/tide-cors" | ||
readme = "README.md" | ||
repository = "https://github.com/rustasync/tide" | ||
license = "MIT OR Apache-2.0" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
futures-preview = "0.3.0-alpha.16" | ||
http = "0.1" | ||
http-service = "0.2.0" | ||
tide-core = { path = "../tide-core" } | ||
|
||
[dev-dependencies] | ||
tide = { path = "../" } | ||
http-service-mock = "0.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# tide-cors | ||
|
||
This crate provides cors-related middleware for Tide. | ||
|
||
## Examples | ||
|
||
Examples are in the `/examples` folder of this crate. | ||
|
||
```rust | ||
#![feature(async_await)] | ||
|
||
use http::header::HeaderValue; | ||
use tide::middleware::CorsMiddleware; | ||
|
||
fn main() { | ||
let mut app = tide::App::new(); | ||
|
||
app.middleware( | ||
CorsMiddleware::new() | ||
.allow_origin(HeaderValue::from_static("*")) | ||
.allow_methods(HeaderValue::from_static("GET, POST, OPTIONS")), | ||
); | ||
|
||
app.at("/").get(async move |_| "Hello, world!"); | ||
|
||
app.run("127.0.0.1:8000").unwrap(); | ||
} | ||
``` | ||
|
||
**Simple Example** | ||
|
||
You can test the simple example by running `cargo run --example cors` while in this crate's directory, and then running this script in the browser console: | ||
|
||
```console | ||
fetch("http://127.0.0.1:8000") | ||
``` | ||
|
||
You will probably get a browser alert when running without cors middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#![feature(async_await)] | ||
|
||
use http::header::HeaderValue; | ||
use tide::middleware::CorsMiddleware; | ||
|
||
fn main() { | ||
let mut app = tide::App::new(); | ||
|
||
app.middleware( | ||
CorsMiddleware::new() | ||
.allow_origin(HeaderValue::from_static("*")) | ||
.allow_methods(HeaderValue::from_static("GET, POST, OPTIONS")), | ||
); | ||
|
||
app.at("/").get(async move |_| "Hello, world!"); | ||
|
||
app.run("127.0.0.1:8000").unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! Cors middleware and extensions for Tide | ||
//! | ||
//! # tide-cors | ||
//! | ||
//! This crate provides cors-related middleware for Tide. | ||
//! | ||
//! ## Examples | ||
//! | ||
//! Examples are in the `/examples` folder of this crate. | ||
//! | ||
//! ```rust, no_run | ||
//! #![feature(async_await)] | ||
//! | ||
//! use http::header::HeaderValue; | ||
//! use tide::middleware::CorsMiddleware; | ||
//! | ||
//! fn main() { | ||
//! let mut app = tide::App::new(); | ||
//! | ||
//! app.middleware( | ||
//! CorsMiddleware::new() | ||
//! .allow_origin(HeaderValue::from_static("*")) | ||
//! .allow_methods(HeaderValue::from_static("GET, POST, OPTIONS")), | ||
//! ); | ||
//! | ||
//! app.at("/").get(async move |_| "Hello, world!"); | ||
//! | ||
//! app.run("127.0.0.1:8000").unwrap(); | ||
//! } | ||
//! ``` | ||
//! | ||
//! **Simple Example** | ||
//! | ||
//! You can test the simple example by running `cargo run --example cors` while in this crate's directory, and then running this script in the browser console: | ||
//! | ||
//! ```console | ||
//! fetch("http://127.0.0.1:8000") | ||
//! ``` | ||
//! | ||
//! You will probably get a browser alert when running without cors middleware | ||
//! | ||
#![feature(async_await)] | ||
#![warn( | ||
nonstandard_style, | ||
rust_2018_idioms, | ||
future_incompatible, | ||
missing_debug_implementations, | ||
missing_docs | ||
)] | ||
|
||
mod middleware; | ||
|
||
pub use self::middleware::CorsMiddleware; |
Oops, something went wrong.