diff --git a/src/main.rs b/src/main.rs index ad86d98..8b8edc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,36 @@ extern crate rocket; use fej_lib::{catchers, ivago}; +// Very temporary solution for CORS +// https://stackoverflow.com/questions/62412361/how-to-set-up-cors-or-options-for-rocket-rs +use rocket::fairing::{Fairing, Info, Kind}; +use rocket::http::Header; +use rocket::{Request, Response}; + +pub struct CORS; + +impl Fairing for CORS { + fn info(&self) -> Info { + Info { + name: "Add CORS headers to responses", + kind: Kind::Response, + } + } + + fn on_response(&self, _: &Request, response: &mut Response) { + response.set_header(Header::new("Access-Control-Allow-Origin", "*")); + response.set_header(Header::new( + "Access-Control-Allow-Methods", + "POST, GET, PATCH, OPTIONS", + )); + response.set_header(Header::new("Access-Control-Allow-Headers", "*")); + response.set_header(Header::new("Access-Control-Allow-Credentials", "true")); + } +} + fn rocket() -> rocket::Rocket { rocket::ignite() + .attach(CORS) .mount("/ivago", ivago::routes()) .register(catchers![catchers::not_found]) }