fix clippy & rustfmt

This commit is contained in:
2022-07-06 17:56:11 +08:00
parent 1aa86b0963
commit 25649e6280
18 changed files with 123 additions and 137 deletions

View File

@@ -3,12 +3,12 @@ use rocket::http::Header;
use rocket::{Request, Response};
use std::path::PathBuf;
pub struct CORS {
pub struct Cors {
pub whitelist: Vec<String>,
}
#[rocket::async_trait]
impl Fairing for CORS {
impl Fairing for Cors {
fn info(&self) -> Info {
Info {
name: "Add CORS headers to responses",
@@ -17,23 +17,22 @@ impl Fairing for CORS {
}
async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) {
request
if let Some(origin) = request
.headers()
.get_one("Origin")
.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, 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(())
});
{
response.set_header(Header::new("Access-Control-Allow-Origin", origin));
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",
));
}
}
}