rocket_cors/examples/fairing.rs

35 lines
854 B
Rust

#![feature(proc_macro_hygiene, decl_macro)]
use std::error::Error;
use rocket::http::Method;
use rocket::{get, routes};
use rocket_cors::{AllowedHeaders, AllowedOrigins};
#[get("/")]
fn cors<'a>() -> &'a str {
"Hello CORS"
}
#[rocket::main]
async fn main() -> Result<(), Box<dyn Error>> {
let allowed_origins = AllowedOrigins::some_exact(&["https://www.acme.com"]);
// You can also deserialize this
let cors = rocket_cors::CorsOptions {
allowed_origins,
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
allow_credentials: true,
..Default::default()
}
.to_cors()?;
rocket::ignite()
.mount("/", routes![cors])
.attach(cors)
.launch()
.await?;
Ok(())
}