Skip to content

Commit

Permalink
collector: permit cross origin requests (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 authored Feb 26, 2024
1 parent f3c72a1 commit dd48ffb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
30 changes: 30 additions & 0 deletions crates/ott-collector/src/cors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header;
use rocket::response::status::NoContent;
use rocket::{Request, Response};

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", "*"));
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
response.set_header(Header::new("Access-Control-Expose-Headers", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
response.set_header(Header::new("Access-Control-Max-Age", "7200"));
}
}

#[options("/<_..>")]
pub fn handle_preflight() -> NoContent {
NoContent
}
6 changes: 4 additions & 2 deletions crates/ott-collector/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[macro_use]
extern crate rocket;

mod cors;

/// Serve the current system state
#[get("/state")]
fn serve_state() {
Expand All @@ -12,8 +14,8 @@ fn rocket() -> _ {
// TODO: spawn discovery tokio task here

rocket::build()
.mount("/", routes![status])
.mount("/", routes![serve_state])
.attach(cors::Cors)
.mount("/", routes![status, cors::handle_preflight, serve_state])
}

#[get("/status")]
Expand Down

0 comments on commit dd48ffb

Please sign in to comment.