Split routes into components

This commit is contained in:
Lambda 2024-09-21 13:30:54 +00:00 committed by Charles Hall
parent b0d1cc1b63
commit d62d0e2f0e
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF

View file

@ -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<ServerHandle>) {
let ctrl_c = async {
signal::ctrl_c().await.expect("failed to install Ctrl+C handler");