diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index ad58d439bf..ac27b9774e 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -246,13 +246,11 @@ impl Server { .route("/blockhash/{height}", get(r::block_hash_from_height_string)) .route("/blockheight", get(r::blockheight_string)) .route("/blocktime", get(r::blocktime_string)) - .route("/content/{inscription_id}", get(r::content)) .route("/r/blockhash", get(r::blockhash)) .route("/r/blockhash/{height}", get(r::blockhash_at_height)) .route("/r/blockheight", get(r::blockheight_string)) .route("/r/blockinfo/{query}", get(r::blockinfo)) .route("/r/blocktime", get(r::blocktime_string)) - .route("/r/children/{inscription_id}", get(r::children)) .route( "/r/children/{inscription_id}/inscriptions", get(r::children_inscriptions), @@ -261,12 +259,6 @@ impl Server { "/r/children/{inscription_id}/inscriptions/{page}", get(r::children_inscriptions_paginated), ) - .route( - "/r/children/{inscription_id}/{page}", - get(r::children_paginated), - ) - .route("/r/inscription/{inscription_id}", get(r::inscription)) - .route("/r/metadata/{inscription_id}", get(r::metadata)) .route("/r/parents/{inscription_id}", get(r::parents)) .route( "/r/parents/{inscription_id}/{page}", @@ -286,6 +278,19 @@ impl Server { ) .route("/r/utxo/{outpoint}", get(Self::utxo)); + let proxiable_routes = Router::new() + .route("/content/{inscription_id}", get(r::content)) + .route("/r/children/{inscription_id}", get(r::children)) + .route( + "/r/children/{inscription_id}/{page}", + get(r::children_paginated), + ) + .route("/r/inscription/{inscription_id}", get(r::inscription)) + .route("/r/metadata/{inscription_id}", get(r::metadata)) + .layer(axum::middleware::from_fn(Self::proxy_fallback)); + + let router = router.merge(proxiable_routes); + let router = router .fallback(Self::fallback) .layer(Extension(index)) @@ -515,6 +520,27 @@ impl Server { Ok(acceptor) } + async fn proxy_fallback( + Extension(server_config): Extension>, + request: http::Request, + next: axum::middleware::Next, + ) -> ServerResult { + let mut path = request.uri().path().to_string(); + + path.remove(0); // remove leading slash + + let response = next.run(request).await; + let status = response.status(); + + if let Some(proxy) = server_config.proxy.as_ref() { + if status == StatusCode::NOT_FOUND { + return task::block_in_place(|| Server::proxy(proxy, &path)); + } + } + + Ok(response) + } + fn index_height(index: &Index) -> ServerResult { index.block_height()?.ok_or_not_found(|| "genesis block") } diff --git a/src/subcommand/server/r.rs b/src/subcommand/server/r.rs index 5f18974571..1eb00c217f 100644 --- a/src/subcommand/server/r.rs +++ b/src/subcommand/server/r.rs @@ -148,15 +148,9 @@ pub(super) async fn blocktime_string( pub(super) async fn children( Extension(index): Extension>, - Extension(server_config): Extension>, Path(inscription_id): Path, ) -> ServerResult { - children_paginated( - Extension(index), - Extension(server_config), - Path((inscription_id, 0)), - ) - .await + children_paginated(Extension(index), Path((inscription_id, 0))).await } pub(super) async fn children_inscriptions( @@ -220,19 +214,14 @@ pub(super) async fn children_inscriptions_paginated( pub(super) async fn children_paginated( Extension(index): Extension>, - Extension(server_config): Extension>, Path((parent, page)): Path<(InscriptionId, usize)>, ) -> ServerResult { task::block_in_place(|| { let Some(parent) = index.get_inscription_entry(parent)? else { - return if let Some(proxy) = server_config.proxy.as_ref() { - Server::proxy(proxy, &format!("r/children/{}/{}", parent, page)) - } else { - Err(ServerError::NotFound(format!( - "inscription {} not found", - parent - ))) - }; + return Err(ServerError::NotFound(format!( + "inscription {} not found", + parent + ))); }; let parent_sequence_number = parent.sequence_number; @@ -257,13 +246,9 @@ pub(super) async fn content( } let Some(mut inscription) = index.get_inscription_by_id(inscription_id)? else { - return if let Some(proxy) = server_config.proxy.as_ref() { - Server::proxy(proxy, &format!("content/{}", inscription_id)) - } else { - Err(ServerError::NotFound(format!( - "inscription {inscription_id} not found" - ))) - }; + return Err(ServerError::NotFound(format!( + "inscription {inscription_id} not found" + ))); }; if let Some(delegate) = inscription.delegate() { @@ -357,14 +342,10 @@ pub(super) async fn inscription( ) -> ServerResult { task::block_in_place(|| { let Some(inscription) = index.get_inscription_by_id(inscription_id)? else { - return if let Some(proxy) = server_config.proxy.as_ref() { - Server::proxy(proxy, &format!("r/inscription/{}", inscription_id)) - } else { - Err(ServerError::NotFound(format!( - "inscription {} not found", - inscription_id - ))) - }; + return Err(ServerError::NotFound(format!( + "inscription {} not found", + inscription_id + ))); }; let entry = index @@ -424,19 +405,14 @@ pub(super) async fn inscription( pub(super) async fn metadata( Extension(index): Extension>, - Extension(server_config): Extension>, Path(inscription_id): Path, ) -> ServerResult { task::block_in_place(|| { let Some(inscription) = index.get_inscription_by_id(inscription_id)? else { - return if let Some(proxy) = server_config.proxy.as_ref() { - Server::proxy(proxy, &format!("r/metadata/{}", inscription_id)) - } else { - Err(ServerError::NotFound(format!( - "inscription {} not found", - inscription_id - ))) - }; + return Err(ServerError::NotFound(format!( + "inscription {} not found", + inscription_id + ))); }; let metadata = inscription