2020-09-08 02:47:01 +00:00
|
|
|
use std::error::Error;
|
2017-07-18 10:22:20 +00:00
|
|
|
|
|
|
|
use rocket::http::Method;
|
2018-10-31 02:59:01 +00:00
|
|
|
use rocket::{get, routes};
|
2020-09-08 02:47:01 +00:00
|
|
|
use rocket_cors::{AllowedHeaders, AllowedOrigins};
|
2017-07-18 10:22:20 +00:00
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn cors<'a>() -> &'a str {
|
|
|
|
"Hello CORS"
|
|
|
|
}
|
|
|
|
|
2020-09-08 02:47:01 +00:00
|
|
|
#[rocket::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn 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
|
|
|
|
2021-04-25 03:26:48 +00:00
|
|
|
rocket::build()
|
2017-07-18 10:22:20 +00:00
|
|
|
.mount("/", routes![cors])
|
2018-12-19 00:29:26 +00:00
|
|
|
.attach(cors)
|
2020-09-08 02:47:01 +00:00
|
|
|
.launch()
|
|
|
|
.await?;
|
2018-12-19 00:29:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
2017-07-18 10:22:20 +00:00
|
|
|
}
|