rocket_cors/examples/json.rs

43 lines
1.4 KiB
Rust
Raw Normal View History

//! This example is to demonstrate the JSON serialization and deserialization of the Cors settings
//!
//! Note: This requires the `serialization` feature which is enabled by default.
#![feature(proc_macro_hygiene, decl_macro)]
use rocket_cors as cors;
use serde_json;
use crate::cors::{AllowedHeaders, AllowedOrigins, CorsOptions};
2018-07-18 05:26:33 +00:00
use rocket::http::Method;
fn main() {
// The default demonstrates the "All" serialization of several of the settings
let default: CorsOptions = Default::default();
2019-03-18 02:13:15 +00:00
let allowed_origins =
AllowedOrigins::some(&["https://www.acme.com"], &["^https://(.+).acme.com$"]);
let options = cors::CorsOptions {
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(),
fairing_route_rank: 0,
};
println!("Default settings");
println!("{}", serde_json::to_string_pretty(&default).unwrap());
println!("Defined settings");
println!("{}", serde_json::to_string_pretty(&options).unwrap());
}