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-07-24 05:11:10 +00:00
|
|
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
use rocket::http::Method;
|
|
|
|
use rocket::response::Responder;
|
2018-10-31 02:59:01 +00:00
|
|
|
use rocket::{get, options, routes, Response, State};
|
2018-12-19 00:29:26 +00:00
|
|
|
use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors, CorsOptions};
|
2017-07-24 05:11:10 +00:00
|
|
|
|
|
|
|
/// Using a borrowed Cors
|
2018-12-19 00:29:26 +00:00
|
|
|
///
|
|
|
|
/// You might want to borrow the `Cors` struct from Rocket's state, for example. Unless you have
|
|
|
|
/// special handling, you might want to use the Guard method instead which has less hassle.
|
|
|
|
///
|
2018-02-14 05:37:48 +00:00
|
|
|
/// Note that the `'r` lifetime annotation is not requred here because `State` borrows with lifetime
|
|
|
|
/// `'r` and so does `Responder`!
|
2017-07-24 05:11:10 +00:00
|
|
|
#[get("/")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn borrowed(options: State<'_, Cors>) -> impl Responder<'_> {
|
2018-02-14 05:22:43 +00:00
|
|
|
options
|
|
|
|
.inner()
|
|
|
|
.respond_borrowed(|guard| guard.responder("Hello CORS"))
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Using a `Response` instead of a `Responder`. You generally won't have to do this.
|
2018-02-14 05:37:48 +00:00
|
|
|
/// Note that the `'r` lifetime annotation is not requred here because `State` borrows with lifetime
|
|
|
|
/// `'r` and so does `Responder`!
|
2017-07-24 05:11:10 +00:00
|
|
|
#[get("/response")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn response(options: State<'_, Cors>) -> impl Responder<'_> {
|
2017-07-24 05:11:10 +00:00
|
|
|
let mut response = Response::new();
|
|
|
|
response.set_sized_body(Cursor::new("Hello CORS!"));
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
options
|
|
|
|
.inner()
|
|
|
|
.respond_borrowed(move |guard| guard.response(response))
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create and use an ad-hoc Cors
|
2018-02-14 05:37:48 +00:00
|
|
|
/// Note that the `'r` lifetime is needed because the compiler cannot elide anything.
|
2018-12-19 00:29:26 +00:00
|
|
|
///
|
|
|
|
/// This is the most likely scenario when you want to have manual CORS validation. You can use this
|
|
|
|
/// when the settings you want to use for a route is not the same as the rest of the application
|
|
|
|
/// (which you might have put in Rocket's state).
|
2017-07-24 05:11:10 +00:00
|
|
|
#[get("/owned")]
|
|
|
|
fn owned<'r>() -> impl Responder<'r> {
|
2018-12-19 00:29:26 +00:00
|
|
|
let options = cors_options().to_cors()?;
|
2017-07-24 05:11:10 +00:00
|
|
|
options.respond_owned(|guard| guard.responder("Hello CORS"))
|
|
|
|
}
|
|
|
|
|
2017-07-24 07:45:53 +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.
|
2017-07-24 05:11:10 +00:00
|
|
|
/// These routes can just return the unit type `()`
|
2018-02-14 05:37:48 +00:00
|
|
|
/// Note that the `'r` lifetime is needed because the compiler cannot elide anything.
|
2017-07-24 05:11:10 +00:00
|
|
|
#[options("/owned")]
|
|
|
|
fn owned_options<'r>() -> impl Responder<'r> {
|
2018-12-19 00:29:26 +00:00
|
|
|
let options = cors_options().to_cors()?;
|
2017-07-24 05:11:10 +00:00
|
|
|
options.respond_owned(|guard| guard.responder(()))
|
|
|
|
}
|
|
|
|
|
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-07-24 05:11:10 +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-07-24 05:11:10 +00:00
|
|
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
|
|
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
|
|
|
allow_credentials: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
rocket::ignite()
|
2018-10-31 02:30:10 +00:00
|
|
|
.mount("/", routes![borrowed, response, owned, owned_options,])
|
2017-07-24 07:45:53 +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-07-24 05:11:10 +00:00
|
|
|
.launch();
|
|
|
|
}
|