Update examples

This commit is contained in:
Yong Wen Chua 2018-12-18 17:43:13 +08:00
parent a5c6208405
commit f7ae2e65d2
No known key found for this signature in database
GPG Key ID: EDC57EEC439CF10B
2 changed files with 13 additions and 9 deletions

View File

@ -4,14 +4,14 @@ use rocket_cors;
use rocket::http::Method;
use rocket::{get, routes};
use rocket_cors::{AllowedHeaders, AllowedOrigins};
use rocket_cors::{AllowedHeaders, AllowedOrigins, Error};
#[get("/")]
fn cors<'a>() -> &'a str {
"Hello CORS"
}
fn main() {
fn main() -> Result<(), Error> {
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["https://www.acme.com"]);
assert!(failed_origins.is_empty());
@ -23,11 +23,12 @@ fn main() {
allow_credentials: true,
..Default::default()
}
.to_cors()
.expect("To not fail");
.to_cors()?;
rocket::ignite()
.mount("/", routes![cors])
.attach(cors)
.launch();
Ok(())
}

View File

@ -7,7 +7,7 @@ use std::io::Cursor;
use rocket::http::Method;
use rocket::Response;
use rocket::{get, options, routes};
use rocket_cors::{AllowedHeaders, AllowedOrigins, Guard, Responder};
use rocket_cors::{AllowedHeaders, AllowedOrigins, Error, Guard, Responder};
/// Using a `Responder` -- the usual way you would use this
#[get("/")]
@ -35,18 +35,19 @@ fn manual(cors: Guard<'_>) -> Responder<'_, &str> {
cors.responder("Manual OPTIONS preflight handling")
}
fn main() {
fn main() -> Result<(), Error> {
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["https://www.acme.com"]);
assert!(failed_origins.is_empty());
// You can also deserialize this
let options = rocket_cors::CorsOptions {
let cors = rocket_cors::CorsOptions {
allowed_origins: allowed_origins,
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
allow_credentials: true,
..Default::default()
};
}
.to_cors()?;
rocket::ignite()
.mount("/", routes![responder, response])
@ -54,6 +55,8 @@ fn main() {
.mount("/", rocket_cors::catch_all_options_routes())
// You can also manually mount an OPTIONS route that will be used instead
.mount("/", routes![manual, manual_options])
.manage(options.to_cors().expect("Not to fail"))
.manage(cors)
.launch();
Ok(())
}