2017-07-19 04:25:56 +00:00
|
|
|
//! This example is to demonstrate the JSON serialization and deserialization of the Cors settings
|
2017-09-05 06:11:28 +00:00
|
|
|
//!
|
|
|
|
//! Note: This requires the `serialization` feature which is enabled by default.
|
2018-10-31 02:25:10 +00:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
2018-11-21 04:17:40 +00:00
|
|
|
|
|
|
|
use rocket_cors as cors;
|
|
|
|
use serde_json;
|
2017-07-19 04:25:56 +00:00
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
use crate::cors::{AllowedHeaders, AllowedOrigins, CorsOptions};
|
2018-07-18 05:26:33 +00:00
|
|
|
use rocket::http::Method;
|
2017-07-19 04:25:56 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// The default demonstrates the "All" serialization of several of the settings
|
2018-12-19 00:29:26 +00:00
|
|
|
let default: CorsOptions = Default::default();
|
2017-07-19 04:25:56 +00:00
|
|
|
|
2018-12-19 03:08:30 +00:00
|
|
|
let allowed_origins = AllowedOrigins::some(&["https://www.acme.com"]);
|
2017-07-19 04:25:56 +00:00
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
let options = cors::CorsOptions {
|
2017-07-19 04:25:56 +00:00
|
|
|
allowed_origins: allowed_origins,
|
|
|
|
allowed_methods: vec![Method::Get, Method::Post, Method::Delete]
|
|
|
|
.into_iter()
|
|
|
|
.map(From::from)
|
|
|
|
.collect(),
|
|
|
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
|
|
|
allow_credentials: true,
|
|
|
|
expose_headers: ["Content-Type", "X-Custom"]
|
|
|
|
.iter()
|
|
|
|
.map(ToString::to_string)
|
|
|
|
.collect(),
|
|
|
|
max_age: Some(42),
|
|
|
|
send_wildcard: false,
|
|
|
|
fairing_route_base: "/mycors".to_string(),
|
2018-02-14 06:21:50 +00:00
|
|
|
fairing_route_rank: 0,
|
2017-07-19 04:25:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
println!("Default settings");
|
|
|
|
println!("{}", serde_json::to_string_pretty(&default).unwrap());
|
|
|
|
|
|
|
|
println!("Defined settings");
|
|
|
|
println!("{}", serde_json::to_string_pretty(&options).unwrap());
|
|
|
|
}
|