fix: handle preflight
This commit is contained in:
13
src/cors.rs
13
src/cors.rs
@@ -1,6 +1,7 @@
|
|||||||
use rocket::fairing::{Fairing, Info, Kind};
|
use rocket::fairing::{Fairing, Info, Kind};
|
||||||
use rocket::http::Header;
|
use rocket::http::Header;
|
||||||
use rocket::{Request, Response};
|
use rocket::{Request, Response};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub struct CORS {
|
pub struct CORS {
|
||||||
pub whitelist: Vec<String>,
|
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| self.whitelist.contains(&origin.to_string()).then(|| origin))
|
||||||
.and_then(|origin| {
|
.and_then(|origin| {
|
||||||
response.set_header(Header::new("Access-Control-Allow-Origin", 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-Credentials", "true"));
|
||||||
|
response.set_header(Header::new(
|
||||||
|
"Access-Control-Allow-Headers",
|
||||||
|
"User-Token, Content-Type",
|
||||||
|
));
|
||||||
Some(())
|
Some(())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[options("/<_path..>")]
|
||||||
|
pub async fn options_handler(_path: PathBuf) {}
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ async fn main() -> Result<(), rocket::Error> {
|
|||||||
api::operation::set_auto_block,
|
api::operation::set_auto_block,
|
||||||
api::vote::vote,
|
api::vote::vote,
|
||||||
api::upload::ipfs_upload,
|
api::upload::ipfs_upload,
|
||||||
|
cors::options_handler,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.mount(
|
.mount(
|
||||||
|
|||||||
Reference in New Issue
Block a user