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 rocket::http::Method;
|
2018-10-31 02:59:01 +00:00
|
|
|
use rocket::{get, routes};
|
2018-12-19 00:29:26 +00:00
|
|
|
use rocket_cors::{AllowedHeaders, AllowedOrigins, Error};
|
2017-07-18 10:22:20 +00:00
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn cors<'a>() -> &'a str {
|
|
|
|
"Hello CORS"
|
|
|
|
}
|
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
fn main() -> Result<(), Error> {
|
2019-03-12 07:05:40 +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 {
|
2019-03-12 07:05:40 +00:00
|
|
|
allowed_origins,
|
2017-07-18 10:22:20 +00:00
|
|
|
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()
|
|
|
|
.mount("/", routes![cors])
|
2018-12-19 00:29:26 +00:00
|
|
|
.attach(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
|
|
|
}
|