rocket_cors/examples/fairing.rs

34 lines
820 B
Rust
Raw Normal View History

#![feature(proc_macro_hygiene, decl_macro)]
use rocket;
use rocket_cors;
use rocket::http::Method;
use rocket::{get, routes};
use rocket_cors::{AllowedHeaders, AllowedOrigins, Error};
#[get("/")]
fn cors<'a>() -> &'a str {
"Hello CORS"
}
fn main() -> Result<(), Error> {
2019-03-12 06:00:34 +00:00
let allowed_origins = AllowedOrigins::some_exact(&["https://www.acme.com"]);
// You can also deserialize this
let cors = rocket_cors::CorsOptions {
2019-03-12 06:17:52 +00:00
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();
Ok(())
}