Skip to content

Commit 0adcc9a

Browse files
committed
collector: permit cross origin requests
1 parent f3c72a1 commit 0adcc9a

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

crates/ott-collector/src/cors.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use rocket::fairing::{Fairing, Info, Kind};
2+
use rocket::http::Header;
3+
use rocket::response::status::NoContent;
4+
use rocket::{Request, Response};
5+
6+
pub struct Cors;
7+
8+
#[rocket::async_trait]
9+
impl Fairing for Cors {
10+
fn info(&self) -> Info {
11+
Info {
12+
name: "Add CORS headers to responses",
13+
kind: Kind::Response,
14+
}
15+
}
16+
17+
async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
18+
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
19+
response.set_header(Header::new("Access-Control-Allow-Methods", "*"));
20+
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
21+
response.set_header(Header::new("Access-Control-Expose-Headers", "*"));
22+
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
23+
response.set_header(Header::new("Access-Control-Max-Age", "7200"));
24+
}
25+
}
26+
27+
#[options("/<_..>")]
28+
pub fn handle_preflight() -> NoContent {
29+
NoContent
30+
}

crates/ott-collector/src/main.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[macro_use]
22
extern crate rocket;
33

4+
mod cors;
5+
46
/// Serve the current system state
57
#[get("/state")]
68
fn serve_state() {
@@ -12,8 +14,8 @@ fn rocket() -> _ {
1214
// TODO: spawn discovery tokio task here
1315

1416
rocket::build()
15-
.mount("/", routes![status])
16-
.mount("/", routes![serve_state])
17+
.attach(cors::Cors)
18+
.mount("/", routes![status, cors::handle_preflight, serve_state])
1719
}
1820

1921
#[get("/status")]

0 commit comments

Comments
 (0)