Skip to content

Commit

Permalink
Use middleware for proxied endpoints (#4204)
Browse files Browse the repository at this point in the history
  • Loading branch information
arik-so authored Jan 29, 2025
1 parent ca9950a commit f05ed9b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
42 changes: 34 additions & 8 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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}",
Expand All @@ -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))
Expand Down Expand Up @@ -515,6 +520,27 @@ impl Server {
Ok(acceptor)
}

async fn proxy_fallback(
Extension(server_config): Extension<Arc<ServerConfig>>,
request: http::Request<axum::body::Body>,
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<Height> {
index.block_height()?.ok_or_not_found(|| "genesis block")
}
Expand Down
56 changes: 16 additions & 40 deletions src/subcommand/server/r.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,9 @@ pub(super) async fn blocktime_string(

pub(super) async fn children(
Extension(index): Extension<Arc<Index>>,
Extension(server_config): Extension<Arc<ServerConfig>>,
Path(inscription_id): Path<InscriptionId>,
) -> 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(
Expand Down Expand Up @@ -220,19 +214,14 @@ pub(super) async fn children_inscriptions_paginated(

pub(super) async fn children_paginated(
Extension(index): Extension<Arc<Index>>,
Extension(server_config): Extension<Arc<ServerConfig>>,
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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -424,19 +405,14 @@ pub(super) async fn inscription(

pub(super) async fn metadata(
Extension(index): Extension<Arc<Index>>,
Extension(server_config): Extension<Arc<ServerConfig>>,
Path(inscription_id): Path<InscriptionId>,
) -> 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
Expand Down

0 comments on commit f05ed9b

Please sign in to comment.