Browse Source

fix: handle preflight

master
hole-thu 3 years ago
parent
commit
462071da54
  1. 13
      src/cors.rs
  2. 1
      src/main.rs

13
src/cors.rs

@ -1,6 +1,7 @@
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header;
use rocket::{Request, Response};
use std::path::PathBuf;
pub struct CORS {
pub whitelist: Vec<String>,
@ -22,9 +23,19 @@ impl Fairing for CORS {
.and_then(|origin| self.whitelist.contains(&origin.to_string()).then(|| origin))
.and_then(|origin| {
response.set_header(Header::new("Access-Control-Allow-Origin", origin));
response.set_header(Header::new("Access-Control-Allow-Methods", "POST, GET"));
response.set_header(Header::new(
"Access-Control-Allow-Methods",
"POST, GET, OPTIONS",
));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
response.set_header(Header::new(
"Access-Control-Allow-Headers",
"User-Token, Content-Type",
));
Some(())
});
}
}
#[options("/<_path..>")]
pub async fn options_handler(_path: PathBuf) {}

1
src/main.rs

@ -81,6 +81,7 @@ async fn main() -> Result<(), rocket::Error> {
api::operation::set_auto_block,
api::vote::vote,
api::upload::ipfs_upload,
cors::options_handler,
],
)
.mount(

Loading…
Cancel
Save