Skip to content
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
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ repository = "https://github.com/rustasync/tide"
version = "0.2.0"

[features]
default = ["hyper", "cookies"]
default = ["hyper", "cookies", "cors"]
cookies = ["tide-cookies"]
cors = ["tide-cors"]
hyper = ["tide-core/http-service-hyper"]

[dependencies]
futures-preview = "0.3.0-alpha.16"
http = "0.1"
http-service = "0.2.0"
tide-cookies = { path = "./tide-cookies", optional = true }
tide-cors = { path = "./tide-cors", optional = true }
tide-core = { path = "./tide-core" }
tide-headers = { path = "./tide-headers" }
tide-log = { path = "./tide-log" }
Expand Down Expand Up @@ -55,6 +57,7 @@ members = [
"tide-compression",
"tide-cookies",
"tide-core",
"tide-cors",
"tide-forms",
"tide-headers",
"tide-log",
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub use http;
#[doc(inline)]
pub use tide_cookies as cookies;

#[cfg(feature = "cors")]
#[doc(inline)]
pub use tide_cors as cors;

#[doc(inline)]
pub use tide_core::{
err_fmt,
Expand Down Expand Up @@ -57,6 +61,9 @@ pub mod middleware {
pub use tide_headers::DefaultHeaders;
pub use tide_log::RequestLogger;

#[cfg(feature = "cors")]
pub use tide_cors::CorsMiddleware;

#[cfg(feature = "cookies")]
pub use tide_cookies::CookiesMiddleware;
}
24 changes: 24 additions & 0 deletions tide-cors/Cargo.toml
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"
38 changes: 38 additions & 0 deletions tide-cors/README.md
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
18 changes: 18 additions & 0 deletions tide-cors/examples/cors.rs
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();
}
54 changes: 54 additions & 0 deletions tide-cors/src/lib.rs
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;
Loading