|
| 1 | +use axum::extract::DefaultBodyLimit; |
| 2 | +use axum::http::StatusCode; |
| 3 | +use axum::response::IntoResponse; |
| 4 | +use axum::routing::{MethodRouter, post}; |
| 5 | +use relay_config::Config; |
| 6 | +use relay_dynamic_config::Feature; |
| 7 | + |
| 8 | +use crate::endpoints::common; |
| 9 | +use crate::envelope::ContentType; |
| 10 | +use crate::extractors::{IntegrationBuilder, RawContentType}; |
| 11 | +use crate::integrations::{LogsIntegration, VercelLogDrainFormat}; |
| 12 | +use crate::service::ServiceState; |
| 13 | + |
| 14 | +/// All routes configured for the Vercel integration. |
| 15 | +/// |
| 16 | +/// The integration currently supports the following endpoints: |
| 17 | +/// - Vercel Log Drain |
| 18 | +pub fn routes(config: &Config) -> axum::Router<ServiceState> { |
| 19 | + axum::Router::new() |
| 20 | + .route("/logs", logs::route(config)) |
| 21 | + .route("/logs/", logs::route(config)) |
| 22 | +} |
| 23 | + |
| 24 | +mod logs { |
| 25 | + use super::*; |
| 26 | + |
| 27 | + async fn handle( |
| 28 | + content_type: RawContentType, |
| 29 | + state: ServiceState, |
| 30 | + builder: IntegrationBuilder, |
| 31 | + ) -> axum::response::Result<impl IntoResponse> { |
| 32 | + let format = match ContentType::from(content_type.as_ref()) { |
| 33 | + ContentType::Json => VercelLogDrainFormat::Json, |
| 34 | + ContentType::NdJson => VercelLogDrainFormat::NdJson, |
| 35 | + _ => return Ok(StatusCode::UNSUPPORTED_MEDIA_TYPE), |
| 36 | + }; |
| 37 | + |
| 38 | + let envelope = builder |
| 39 | + .with_type(LogsIntegration::VercelDrainLog { format }) |
| 40 | + .with_required_feature(Feature::VercelLogDrainEndpoint) |
| 41 | + .build(); |
| 42 | + |
| 43 | + common::handle_envelope(&state, envelope).await?; |
| 44 | + |
| 45 | + Ok(StatusCode::ACCEPTED) |
| 46 | + } |
| 47 | + |
| 48 | + pub fn route(config: &Config) -> MethodRouter<ServiceState> { |
| 49 | + post(handle).route_layer(DefaultBodyLimit::max(config.max_envelope_size())) |
| 50 | + } |
| 51 | +} |
0 commit comments