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