rocket-0.5-dev-cors/src/lib.rs

33 lines
901 B
Rust

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
extern crate async_trait;
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header;
use rocket::{Request, Response};
pub struct CORS();
#[async_trait]
impl Fairing for CORS {
fn info(&self) -> Info {
Info {
name: "Add CORS headers to requests",
kind: Kind::Response,
}
}
#[inline]
async fn on_response<'r>(&self, _req: &'r Request<'_>, res: &mut Response<'r>) {
res.set_header(Header::new("Access-Control-Allow-Origin", "*"));
res.set_header(Header::new(
"Access-Control-Allow-Methods",
"HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS",
));
res.set_header(Header::new("Access-Control-Allow-Headers", "*"));
res.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
}
}