mirror of
https://github.com/TeamPiped/sponsorblock-mirror.git
synced 2024-08-14 23:57:05 +00:00
Add cors for official instance.
This commit is contained in:
parent
e00d754521
commit
cccc095de8
2 changed files with 29 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
|||
[release]
|
||||
address = "0.0.0.0"
|
||||
log_level = "off"
|
||||
|
||||
[debug.databases]
|
||||
sponsorblock = { url = "postgresql://sponsorblock:password123@localhost" }
|
||||
|
|
31
src/main.rs
31
src/main.rs
|
@ -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])
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue