Add cors for official instance.

This commit is contained in:
Kavin 2022-10-25 10:13:34 +01:00
parent e00d754521
commit cccc095de8
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 29 additions and 3 deletions

View file

@ -1,5 +1,6 @@
[release]
address = "0.0.0.0"
log_level = "off"
[debug.databases]
sponsorblock = { url = "postgresql://sponsorblock:password123@localhost" }

View file

@ -6,8 +6,9 @@ use std::thread::sleep;
use std::time::{Duration, Instant, SystemTime};
use diesel::connection::SimpleConnection;
use rocket::{Build, Rocket};
use rocket::fairing::AdHoc;
use rocket::{Build, Request, Response, Rocket};
use rocket::fairing::{AdHoc, Fairing, Info, Kind};
use rocket::http::Header;
use rocket_sync_db_pools::database;
use tokio::time::interval;
@ -38,6 +39,29 @@ async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
rocket
}
pub struct CORS;
#[rocket::async_trait]
impl Fairing for CORS {
fn info(&self) -> Info {
Info {
name: "Add CORS headers to responses",
kind: Kind::Response,
}
}
async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) {
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
response.set_header(Header::new("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS"));
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
if request.method() == rocket::http::Method::Options {
response.set_streamed_body(tokio::io::empty());
response.set_status(rocket::http::Status::Ok);
}
}
}
static mut LAST_UPDATE: Option<SystemTime> = None;
#[launch]
@ -95,5 +119,6 @@ fn rocket() -> Rocket<Build> {
});
})
})
).mount("/", routes![skip_segments])
).attach(CORS)
.mount("/", routes![skip_segments])
}