From d62d0e2f0ec07666e515f1eb2424370a6f94498b Mon Sep 17 00:00:00 2001 From: Lambda Date: Sat, 21 Sep 2024 13:30:54 +0000 Subject: [PATCH] Split routes into components --- src/cli/serve.rs | 103 +++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/src/cli/serve.rs b/src/cli/serve.rs index d8345c11..95ae9b44 100644 --- a/src/cli/serve.rs +++ b/src/cli/serve.rs @@ -249,10 +249,37 @@ async fn unrecognized_method( Ok(inner) } -#[allow(clippy::too_many_lines)] -fn routes(config: &Config) -> Router { +/// Routes for legacy unauthenticated `/_matrix/media/*` APIs (used by both +/// clients and federation) +fn legacy_media_routes(config: &Config) -> Router { + use client_server as c2s; + + let router = Router::new(); + + // deprecated, but unproblematic + let router = router.ruma_route(c2s::get_media_config_legacy_route); + + if config.serve_media_unauthenticated { + router + .ruma_route(c2s::get_content_legacy_route) + .ruma_route(c2s::get_content_as_filename_legacy_route) + .ruma_route(c2s::get_content_thumbnail_legacy_route) + } else { + router + .route( + "/_matrix/media/v3/download/*path", + any(unauthenticated_media_disabled), + ) + .route( + "/_matrix/media/v3/thumbnail/*path", + any(unauthenticated_media_disabled), + ) + } +} + +#[allow(clippy::too_many_lines)] +fn client_routes() -> Router { use client_server as c2s; - use server_server as s2s; let router = Router::new() .ruma_route(c2s::get_supported_versions_route) @@ -345,25 +372,6 @@ fn routes(config: &Config) -> Router { .ruma_route(c2s::turn_server_route) .ruma_route(c2s::send_event_to_device_route); - // deprecated, but unproblematic - let router = router.ruma_route(c2s::get_media_config_legacy_route); - let router = if config.serve_media_unauthenticated { - router - .ruma_route(c2s::get_content_legacy_route) - .ruma_route(c2s::get_content_as_filename_legacy_route) - .ruma_route(c2s::get_content_thumbnail_legacy_route) - } else { - router - .route( - "/_matrix/media/v3/download/*path", - any(unauthenticated_media_disabled), - ) - .route( - "/_matrix/media/v3/thumbnail/*path", - any(unauthenticated_media_disabled), - ) - }; - // authenticated media let router = router .ruma_route(c2s::get_media_config_route) @@ -419,16 +427,7 @@ fn routes(config: &Config) -> Router { .put(c2s::send_state_event_for_empty_key_route), ); - let router = if config.observability.metrics.enable { - router.route( - "/metrics", - get(|| async { observability::METRICS.export() }), - ) - } else { - router - }; - - let router = router + router .route( "/_matrix/client/r0/rooms/:room_id/initialSync", get(initial_sync), @@ -437,15 +436,13 @@ fn routes(config: &Config) -> Router { "/_matrix/client/v3/rooms/:room_id/initialSync", get(initial_sync), ) - .route("/", get(it_works)) - .fallback(not_found); +} - let router = router - .route("/.well-known/matrix/client", get(well_known::client)) - .route("/.well-known/matrix/server", get(well_known::server)); +fn federation_routes(config: &Config) -> Router { + use server_server as s2s; if config.federation.enable { - router + Router::new() .ruma_route(s2s::get_server_version_route) .route("/_matrix/key/v2/server", get(s2s::get_server_keys_route)) .route( @@ -473,12 +470,40 @@ fn routes(config: &Config) -> Router { .ruma_route(s2s::media_download_route) .ruma_route(s2s::media_thumbnail_route) } else { - router + Router::new() .route("/_matrix/federation/*path", any(federation_disabled)) .route("/_matrix/key/*path", any(federation_disabled)) } } +fn metrics_routes(config: &Config) -> Router { + if config.observability.metrics.enable { + Router::new().route( + "/metrics", + get(|| async { observability::METRICS.export() }), + ) + } else { + Router::new() + } +} + +fn well_known_routes() -> Router { + Router::new() + .route("/.well-known/matrix/client", get(well_known::client)) + .route("/.well-known/matrix/server", get(well_known::server)) +} + +fn routes(config: &Config) -> Router { + Router::new() + .merge(client_routes()) + .merge(federation_routes(config)) + .merge(legacy_media_routes(config)) + .merge(well_known_routes()) + .merge(metrics_routes(config)) + .route("/", get(it_works)) + .fallback(not_found) +} + async fn shutdown_signal(handles: Vec) { let ctrl_c = async { signal::ctrl_c().await.expect("failed to install Ctrl+C handler");