fix: handle preflight

This commit is contained in:
2022-05-03 01:11:54 +08:00
parent a42ef851be
commit 462071da54
2 changed files with 13 additions and 1 deletions

View File

@@ -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) {}

View File

@@ -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(