Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit 4b3634b

Browse files
authored
add cors support (#43)
1 parent a841ac4 commit 4b3634b

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ serde_json = "1.0.96"
1515
governor = "0.5.1"
1616
tower_governor = "0.0.4"
1717
time = { version = "0.3.20", features = ["formatting", "macros", "parsing", "serde"] }
18-
tower-http = { version = "0.4.0", features = ["trace"] }
18+
tower-http = { version = "0.4.0", features = ["cors", "trace"] }
1919
bytes = "1"
2020
anyhow = "1.0"
2121
flate2 = "1.0"

capture/src/capture.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ pub async fn event(
104104
}))
105105
}
106106

107+
pub async fn options() -> Result<Json<CaptureResponse>, CaptureError> {
108+
Ok(Json(CaptureResponse {
109+
status: CaptureResponseCode::Ok,
110+
}))
111+
}
112+
107113
pub fn process_single_event(
108114
event: &RawEvent,
109115
context: &ProcessingContext,

capture/src/router.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::future::ready;
22
use std::sync::Arc;
33

4+
use axum::http::Method;
45
use axum::{
56
routing::{get, post},
67
Router,
78
};
9+
use tower_http::cors::{AllowOrigin, Any, CorsLayer};
810
use tower_http::trace::TraceLayer;
911

1012
use crate::{billing_limits::BillingLimiter, capture, redis::Client, sink, time::TimeSource};
@@ -41,12 +43,20 @@ pub fn router<
4143
billing,
4244
};
4345

46+
// Very permissive CORS policy, as old SDK versions
47+
// and reverse proxies might send funky headers.
48+
let cors = CorsLayer::new()
49+
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
50+
.allow_headers(Any)
51+
.allow_origin(AllowOrigin::mirror_request());
52+
4453
let router = Router::new()
4554
// TODO: use NormalizePathLayer::trim_trailing_slash
4655
.route("/", get(index))
47-
.route("/i/v0/e", post(capture::event))
48-
.route("/i/v0/e/", post(capture::event))
56+
.route("/i/v0/e", post(capture::event).options(capture::options))
57+
.route("/i/v0/e/", post(capture::event).options(capture::options))
4958
.layer(TraceLayer::new_for_http())
59+
.layer(cors)
5060
.layer(axum::middleware::from_fn(track_metrics))
5161
.with_state(state);
5262

0 commit comments

Comments
 (0)