Skip to content

Commit

Permalink
Added rocket_cors and inject CORS-Headers from Environment
Browse files Browse the repository at this point in the history
Signed-off-by: steigr <[email protected]>
  • Loading branch information
steigr authored and hoodie committed Feb 17, 2018
1 parent e0f249e commit 0d5ec26
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ rocket_contrib = {version = "0.3", optional = true }
openssl-sys = { version = "0.9", optional = true }
openssl-probe = { version = "0.1.2", optional = true }

# asciii-server CORS-support
rocket_cors = { version = "0.2", optional = true }

bill = {version= "0.2", features=["serialization"]}
icalendar = "0.3"

Expand Down Expand Up @@ -89,7 +92,7 @@ default = ["document_export", "git_statuses", "serialization", "deserialization"
document_export = ["handlebars", "serialization"]
serde_base = ["serde", "serde_derive"]
serialization = ["serde_base", "serde_json"]
server = ["rocket", "rocket_codegen", "rocket_contrib","openssl-sys","openssl-probe" ]
server = ["rocket", "rocket_codegen", "rocket_contrib", "rocket_cors", "openssl-sys", "openssl-probe" ]
deserialization = ["serde_base","serde_yaml", "ordered-float", "num-traits"]
git_statuses = ["git2"]
shell = ["rustyline"]
Expand Down
31 changes: 28 additions & 3 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ extern crate base64;

extern crate openssl_probe;

extern crate rocket_cors;

use rocket::response::NamedFile;
use itertools::Itertools;

Expand All @@ -30,6 +32,9 @@ use std::sync::Mutex;
use std::sync::mpsc::{sync_channel, SyncSender};
use std::thread;

use rocket::http::Method;
use rocket_cors::{AllowedOrigins, AllowedHeaders};

pub struct ProjectLoader {
storage: Storage<Project>,
years: Vec<i32>,
Expand Down Expand Up @@ -295,7 +300,8 @@ fn authorization(api_key: ApiKey) -> content::Json<String> {

fn main() {
openssl_probe::init_ssl_cert_env_vars();
rocket::ignite()

let server = rocket::ignite()
.mount("/", routes![static_files])
.mount("/cal/plain", routes![calendar::cal_plain, calendar::cal_plain_params])
.mount("/cal", routes![calendar::cal, calendar::cal_params])
Expand All @@ -306,6 +312,25 @@ fn main() {
projects::all_full,
projects::by_name,
authorization,
])
.launch();
]);

if let Ok(env_cors) = std::env::var("CORS_ALLOWED_ORIGINS") {

println!("Adding CORS Data {}",env_cors);
let env_allowed_origins = &[env_cors.as_str()];
let (allowed_origins, failed_origins) = AllowedOrigins::some(env_allowed_origins);
assert!(failed_origins.is_empty());
let options = rocket_cors::Cors {
allowed_origins: allowed_origins,
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
allow_credentials: true,
..Default::default()
};

server.attach(options)
} else {
server
}.launch();

}

0 comments on commit 0d5ec26

Please sign in to comment.