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

Commit

Permalink
add cors support (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
xvello authored Oct 31, 2023
1 parent a841ac4 commit 4b3634b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ serde_json = "1.0.96"
governor = "0.5.1"
tower_governor = "0.0.4"
time = { version = "0.3.20", features = ["formatting", "macros", "parsing", "serde"] }
tower-http = { version = "0.4.0", features = ["trace"] }
tower-http = { version = "0.4.0", features = ["cors", "trace"] }
bytes = "1"
anyhow = "1.0"
flate2 = "1.0"
Expand Down
6 changes: 6 additions & 0 deletions capture/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ pub async fn event(
}))
}

pub async fn options() -> Result<Json<CaptureResponse>, CaptureError> {
Ok(Json(CaptureResponse {
status: CaptureResponseCode::Ok,
}))
}

pub fn process_single_event(
event: &RawEvent,
context: &ProcessingContext,
Expand Down
14 changes: 12 additions & 2 deletions capture/src/router.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::future::ready;
use std::sync::Arc;

use axum::http::Method;
use axum::{
routing::{get, post},
Router,
};
use tower_http::cors::{AllowOrigin, Any, CorsLayer};
use tower_http::trace::TraceLayer;

use crate::{billing_limits::BillingLimiter, capture, redis::Client, sink, time::TimeSource};
Expand Down Expand Up @@ -41,12 +43,20 @@ pub fn router<
billing,
};

// Very permissive CORS policy, as old SDK versions
// and reverse proxies might send funky headers.
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
.allow_headers(Any)
.allow_origin(AllowOrigin::mirror_request());

let router = Router::new()
// TODO: use NormalizePathLayer::trim_trailing_slash
.route("/", get(index))
.route("/i/v0/e", post(capture::event))
.route("/i/v0/e/", post(capture::event))
.route("/i/v0/e", post(capture::event).options(capture::options))
.route("/i/v0/e/", post(capture::event).options(capture::options))
.layer(TraceLayer::new_for_http())
.layer(cors)
.layer(axum::middleware::from_fn(track_metrics))
.with_state(state);

Expand Down

0 comments on commit 4b3634b

Please sign in to comment.