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-18 10:22:20 +00:00
|
|
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
use rocket::http::Method;
|
2018-07-18 05:26:33 +00:00
|
|
|
use rocket::Response;
|
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, Error, Guard, Responder};
|
2017-07-18 10:22:20 +00:00
|
|
|
|
|
|
|
/// Using a `Responder` -- the usual way you would use this
|
|
|
|
#[get("/")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn responder(cors: Guard<'_>) -> Responder<'_, &str> {
|
2017-07-18 10:22:20 +00:00
|
|
|
cors.responder("Hello CORS!")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Using a `Response` instead of a `Responder`. You generally won't have to do this.
|
2017-07-24 05:11:10 +00:00
|
|
|
#[get("/response")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn response(cors: Guard<'_>) -> Response<'_> {
|
2017-07-18 10:22:20 +00:00
|
|
|
let mut response = Response::new();
|
|
|
|
response.set_sized_body(Cursor::new("Hello CORS!"));
|
|
|
|
cors.response(response)
|
|
|
|
}
|
|
|
|
|
2017-07-24 07:45:53 +00:00
|
|
|
/// Manually mount an OPTIONS route for your own handling
|
|
|
|
#[options("/manual")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn manual_options(cors: Guard<'_>) -> Responder<'_, &str> {
|
2017-07-24 07:45:53 +00:00
|
|
|
cors.responder("Manual OPTIONS preflight handling")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Manually mount an OPTIONS route for your own handling
|
|
|
|
#[get("/manual")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn manual(cors: Guard<'_>) -> Responder<'_, &str> {
|
2017-07-24 07:45:53 +00:00
|
|
|
cors.responder("Manual OPTIONS preflight handling")
|
2017-07-18 10:22:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
fn main() -> Result<(), Error> {
|
2019-03-12 06:00:34 +00:00
|
|
|
let allowed_origins = AllowedOrigins::some_exact(&["https://www.acme.com"]);
|
2017-07-18 10:22:20 +00:00
|
|
|
|
|
|
|
// You can also deserialize this
|
2018-12-19 00:29:26 +00:00
|
|
|
let cors = rocket_cors::CorsOptions {
|
2017-07-18 10:22:20 +00:00
|
|
|
allowed_origins: allowed_origins,
|
|
|
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
2017-07-19 04:25:56 +00:00
|
|
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
2017-07-18 10:22:20 +00:00
|
|
|
allow_credentials: true,
|
|
|
|
..Default::default()
|
2018-12-19 00:29:26 +00:00
|
|
|
}
|
|
|
|
.to_cors()?;
|
2017-07-18 10:22:20 +00:00
|
|
|
|
|
|
|
rocket::ignite()
|
2018-10-31 02:30:10 +00:00
|
|
|
.mount("/", routes![responder, response])
|
2017-07-24 07:45:53 +00:00
|
|
|
// Mount the routes to catch all the OPTIONS pre-flight requests
|
|
|
|
.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])
|
2018-12-19 00:29:26 +00:00
|
|
|
.manage(cors)
|
2017-07-18 10:22:20 +00:00
|
|
|
.launch();
|
2018-12-19 00:29:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
2017-07-18 10:22:20 +00:00
|
|
|
}
|