relay_server/endpoints/
mod.rs

1//! Web server endpoints.
2//!
3//! This module contains implementations for all supported relay endpoints, as well as a generic
4//! `forward` endpoint that sends unknown requests to the upstream.
5
6mod attachments;
7mod autoscaling;
8mod batch_metrics;
9mod batch_outcomes;
10mod common;
11mod envelope;
12mod forward;
13mod health_check;
14mod integrations;
15mod minidump;
16mod monitor;
17mod nel;
18#[cfg(sentry)]
19mod playstation;
20mod project_configs;
21mod public_keys;
22mod register;
23mod security_report;
24mod statics;
25mod store;
26mod unreal;
27mod upload;
28
29use axum::extract::DefaultBodyLimit;
30use axum::routing::{Router, any, get, post};
31use relay_config::Config;
32
33use crate::middlewares;
34use crate::service::ServiceState;
35use crate::services::upload::UPLOAD_PATCH_PATH;
36
37/// Size limit for internal batch endpoints.
38const BATCH_JSON_BODY_LIMIT: usize = 50_000_000; // 50 MB
39
40/// All of Relay's routes.
41///
42/// This includes [`public_routes`] as well as [`internal_routes`].
43pub fn all_routes(config: &Config) -> Router<ServiceState> {
44    public_routes_raw(config).merge(internal_routes(config))
45}
46
47/// Relay's internal routes.
48///
49/// Routes which do not need to be exposed.
50#[rustfmt::skip]
51pub fn internal_routes(_: &Config) -> Router<ServiceState>{
52    Router::new()
53        .route("/api/relay/healthcheck/{kind}/", get(health_check::handle))
54        .route("/api/relay/autoscaling/", get(autoscaling::handle))
55        // Fallback route, but with a name, and just on `/api/relay/*`.
56        .route("/api/relay/{*not_found}", any(statics::not_found))
57}
58
59/// Relay's public routes.
60///
61/// Routes which are public API and must be exposed.
62pub fn public_routes(config: &Config) -> Router<ServiceState> {
63    // Exclude internal routes, they must be configured separately.
64    public_routes_raw(config).route("/api/relay/{*not_found}", any(statics::not_found))
65}
66
67#[rustfmt::skip]
68fn public_routes_raw(config: &Config) -> Router<ServiceState> {
69    // Sentry Web API routes pointing to /api/0/relays/
70    let web_routes = Router::new()
71        .route("/api/0/relays/projectconfigs/", post(project_configs::handle))
72        .route("/api/0/relays/publickeys/", post(public_keys::handle))
73        .route("/api/0/relays/register/challenge/", post(register::challenge))
74        .route("/api/0/relays/register/response/", post(register::response))
75        // Network connectivity check for downstream Relays, same as the internal health check.
76        .route("/api/0/relays/live/", get(health_check::handle_live))
77        .route_layer(DefaultBodyLimit::max(crate::constants::MAX_JSON_SIZE));
78
79    let batch_routes = Router::new()
80        .route("/api/0/relays/outcomes/", post(batch_outcomes::handle))
81        .route("/api/0/relays/metrics/", post(batch_metrics::handle))
82        .route_layer(DefaultBodyLimit::max(BATCH_JSON_BODY_LIMIT));
83
84    // Ingestion routes pointing to /api/:project_id/
85    let store_routes = Router::new()
86        // Legacy store path that is missing the project parameter.
87        .route("/api/store/", store::route(config))
88        // cron monitor level routes.  These are user facing APIs and as such support trailing slashes.
89        .route("/api/{project_id}/cron/{monitor_slug}/{sentry_key}", monitor::route(config))
90        .route("/api/{project_id}/cron/{monitor_slug}/{sentry_key}/", monitor::route(config))
91        .route("/api/{project_id}/cron/{monitor_slug}", monitor::route(config))
92        .route("/api/{project_id}/cron/{monitor_slug}/", monitor::route(config))
93
94        .route("/api/{project_id}/store/", store::route(config))
95        .route("/api/{project_id}/envelope/", envelope::route(config))
96        .route("/api/{project_id}/security/", security_report::route(config))
97        .route("/api/{project_id}/csp-report/", security_report::route(config))
98        .route("/api/{project_id}/nel/", nel::route(config))
99        // No mandatory trailing slash here because people already use it like this.
100        .route("/api/{project_id}/minidump", minidump::route(config))
101        .route("/api/{project_id}/minidump/", minidump::route(config))
102        .route("/api/{project_id}/events/{event_id}/attachments/", post(attachments::handle))
103        .route("/api/{project_id}/unreal/{sentry_key}/", unreal::route(config))
104        .route("/api/{project_id}/upload/", upload::route_post(config))
105        .route(UPLOAD_PATCH_PATH, upload::route_patch(config));
106
107    #[cfg(sentry)]
108    let store_routes = store_routes.route("/api/{project_id}/playstation/", playstation::route(config));
109    let store_routes = store_routes.route_layer(middlewares::cors());
110
111    // Integration routes.
112    //
113    // For integrations we want to be lenient on trailing `/`, as they are often manually
114    // configured by users or protocols may force a specific variant.
115    let integration_routes = Router::new()
116        .nest("/api/{project_id}/integration/otlp", integrations::otlp::routes(config))
117        .nest("/api/{project_id}/integration/vercel", integrations::vercel::routes(config))
118        .route_layer(middlewares::cors());
119
120    // NOTE: If you add a new (non-experimental) route here, please also list it in
121    // https://github.com/getsentry/sentry-docs/blob/master/docs/product/relay/operating-guidelines.mdx
122
123    Router::new()
124        .merge(web_routes)
125        .merge(batch_routes)
126        .merge(store_routes)
127        .merge(integration_routes)
128        // Forward all other API routes to the upstream. This will 404 for non-API routes.
129        .fallback(forward::forward)
130}