2018-07-16 01:36:07 +00:00
|
|
|
#![feature(plugin)]
|
2017-07-24 05:11:10 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
|
|
|
extern crate rocket;
|
|
|
|
extern crate rocket_cors;
|
|
|
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
use rocket::{Response, State};
|
2017-07-24 05:11:10 +00:00
|
|
|
use rocket::http::Method;
|
|
|
|
use rocket::response::Responder;
|
2018-02-14 05:22:43 +00:00
|
|
|
use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors};
|
2017-07-24 05:11:10 +00:00
|
|
|
|
|
|
|
/// Using a borrowed Cors
|
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-02-14 05:37:48 +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-02-14 05:37:48 +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.
|
2017-07-24 05:11:10 +00:00
|
|
|
#[get("/owned")]
|
|
|
|
fn owned<'r>() -> impl Responder<'r> {
|
|
|
|
let options = cors_options();
|
|
|
|
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> {
|
|
|
|
let options = cors_options();
|
|
|
|
options.respond_owned(|guard| guard.responder(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cors_options() -> Cors {
|
|
|
|
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["https://www.acme.com"]);
|
|
|
|
assert!(failed_origins.is_empty());
|
|
|
|
|
|
|
|
// You can also deserialize this
|
|
|
|
rocket_cors::Cors {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
rocket::ignite()
|
|
|
|
.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
|
2017-07-24 05:11:10 +00:00
|
|
|
.manage(cors_options())
|
|
|
|
.launch();
|
|
|
|
}
|