2017-09-05 06:59:51 +00:00
|
|
|
//! This is an example of how you can mix and match the "Truly manual" mode with "Guard".
|
|
|
|
//!
|
|
|
|
//! In this example, you typically have an application wide `Cors` struct except for one specific
|
|
|
|
//! `ping` route that you want to allow all Origins to access.
|
|
|
|
|
2018-10-31 02:25:10 +00:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
2018-11-21 04:17:40 +00:00
|
|
|
use rocket;
|
|
|
|
use rocket_cors;
|
2017-09-05 06:59:51 +00:00
|
|
|
|
|
|
|
use rocket::http::Method;
|
|
|
|
use rocket::response::Responder;
|
2018-10-31 02:59:01 +00:00
|
|
|
use rocket::{get, options, routes};
|
2018-12-19 00:29:26 +00:00
|
|
|
use rocket_cors::{AllowedHeaders, AllowedOrigins, CorsOptions, Guard};
|
2017-09-05 06:59:51 +00:00
|
|
|
|
|
|
|
/// The "usual" app route
|
|
|
|
#[get("/")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn app(cors: Guard<'_>) -> rocket_cors::Responder<'_, &str> {
|
2017-09-05 06:59:51 +00:00
|
|
|
cors.responder("Hello CORS!")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The special "ping" route
|
|
|
|
#[get("/ping")]
|
|
|
|
fn ping<'r>() -> impl Responder<'r> {
|
2018-12-19 00:29:26 +00:00
|
|
|
let cors = cors_options_all().to_cors()?;
|
|
|
|
cors.respond_owned(|guard| guard.responder("Pong!"))
|
2017-09-05 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// You need to define an OPTIONS route for preflight checks if you want to use `Cors` struct
|
|
|
|
/// that is not in Rocket's managed state.
|
|
|
|
/// These routes can just return the unit type `()`
|
|
|
|
#[options("/ping")]
|
|
|
|
fn ping_options<'r>() -> impl Responder<'r> {
|
2018-12-19 00:29:26 +00:00
|
|
|
let cors = cors_options_all().to_cors()?;
|
|
|
|
cors.respond_owned(|guard| guard.responder(()))
|
2017-09-05 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the "application wide" Cors struct
|
2018-12-19 00:29:26 +00:00
|
|
|
fn cors_options() -> CorsOptions {
|
2019-03-12 06:00:34 +00:00
|
|
|
let allowed_origins = AllowedOrigins::some_exact(&["https://www.acme.com"]);
|
2017-09-05 06:59:51 +00:00
|
|
|
|
|
|
|
// You can also deserialize this
|
2018-12-19 00:29:26 +00:00
|
|
|
rocket_cors::CorsOptions {
|
2019-03-12 06:17:52 +00:00
|
|
|
allowed_origins,
|
2017-09-05 06:59:51 +00:00
|
|
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
|
|
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
|
|
|
allow_credentials: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A special struct that allows all origins
|
|
|
|
///
|
|
|
|
/// Note: In your real application, you might want to use something like `lazy_static` to generate
|
|
|
|
/// a `&'static` reference to this instead of creating a new struct on every request.
|
2018-12-19 00:29:26 +00:00
|
|
|
fn cors_options_all() -> CorsOptions {
|
2017-09-05 06:59:51 +00:00
|
|
|
// You can also deserialize this
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
rocket::ignite()
|
2018-10-31 02:30:10 +00:00
|
|
|
.mount("/", routes![app, ping, ping_options,])
|
2017-09-05 06:59:51 +00:00
|
|
|
.mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes
|
2018-12-19 00:29:26 +00:00
|
|
|
.manage(cors_options().to_cors().expect("To not fail"))
|
2017-09-05 06:59:51 +00:00
|
|
|
.launch();
|
|
|
|
}
|