server: reorg

This commit is contained in:
nym21
2026-04-27 10:35:18 +02:00
parent 7a0b4b5890
commit 07bc2d42b8
17 changed files with 40 additions and 63 deletions

View File

@@ -1,35 +0,0 @@
use aide::axum::ApiRouter;
use crate::AppState;
mod addrs;
mod blocks;
mod fees;
mod general;
mod mempool;
mod mining;
mod transactions;
use addrs::AddrRoutes;
use blocks::BlockRoutes;
use fees::FeesRoutes;
use general::GeneralRoutes;
use mempool::MempoolRoutes;
use mining::MiningRoutes;
use transactions::TxRoutes;
pub trait MempoolSpaceRoutes {
fn add_mempool_space_routes(self) -> Self;
}
impl MempoolSpaceRoutes for ApiRouter<AppState> {
fn add_mempool_space_routes(self) -> Self {
self.add_general_routes()
.add_addr_routes()
.add_block_routes()
.add_mining_routes()
.add_fees_routes()
.add_mempool_routes()
.add_tx_routes()
}
}

View File

@@ -14,24 +14,36 @@ use axum::{
use crate::{
Error,
api::{
mempool_space::MempoolSpaceRoutes, metrics::ApiMetricsLegacyRoutes,
series::ApiSeriesRoutes, series_legacy::ApiSeriesLegacyRoutes, server::ServerRoutes,
urpd::ApiUrpdRoutes,
metrics::ApiMetricsLegacyRoutes, series::ApiSeriesRoutes,
series_legacy::ApiSeriesLegacyRoutes, server::ServerRoutes, urpd::ApiUrpdRoutes,
},
extended::{ResponseExtended, TransformResponseExtended},
};
use super::AppState;
mod mempool_space;
mod addrs;
mod blocks;
mod fees;
mod general;
mod mempool;
mod metrics;
mod mining;
mod openapi;
mod series;
mod series_legacy;
mod server;
mod transactions;
mod urpd;
use addrs::AddrRoutes;
use blocks::BlockRoutes;
use fees::FeesRoutes;
use general::GeneralRoutes;
use mempool::MempoolRoutes;
use mining::MiningRoutes;
pub use openapi::*;
use transactions::TxRoutes;
pub trait ApiRoutes {
fn add_api_routes(self) -> Self;
@@ -44,7 +56,13 @@ impl ApiRoutes for ApiRouter<AppState> {
.add_series_legacy_routes()
.add_urpd_routes()
.add_metrics_legacy_routes()
.add_mempool_space_routes()
.add_general_routes()
.add_addr_routes()
.add_block_routes()
.add_mining_routes()
.add_fees_routes()
.add_mempool_routes()
.add_tx_routes()
.route("/api/server", get(Redirect::temporary("/api#tag/server")))
.api_route(
"/openapi.json",

View File

@@ -17,4 +17,3 @@ pub use params::CacheParams;
pub use strategy::CacheStrategy;
pub(crate) use mode::init;
pub(crate) use params::CC_ERROR;

View File

@@ -14,7 +14,7 @@ const CC: &str = "public, no-cache, stale-if-error=86400";
// Errors: short, must-revalidate, no `stale-if-error` (we don't want a 24h-old
// error served when origin recovers). Same string for browser and CDN.
pub(crate) const CC_ERROR: &str = "public, max-age=1, must-revalidate";
const CC_ERROR: &str = "public, max-age=1, must-revalidate";
/// Resolved cache parameters: an ETag plus the two Cache-Control directives.
pub struct CacheParams {
@@ -97,6 +97,13 @@ impl CacheParams {
}
}
/// Apply error cache-control headers without an ETag. Used for synthesized
/// errors (panics, fallback handlers) that have no resource etag.
pub fn apply_error_cache_control(headers: &mut HeaderMap) {
headers.insert_cache_control(CC_ERROR);
headers.insert_cdn_cache_control(CC_ERROR);
}
pub fn matches_etag(&self, headers: &HeaderMap) -> bool {
headers.has_etag(self.etag.as_str())
}

View File

@@ -7,11 +7,7 @@ use brk_error::Error as BrkError;
use schemars::JsonSchema;
use serde::Serialize;
use crate::{
cache::{CC_ERROR, CacheParams},
etag::Etag,
extended::HeaderMapExtended,
};
use crate::{cache::CacheParams, etag::Etag};
/// Server result type with Error that implements IntoResponse.
pub type Result<T> = std::result::Result<T, Error>;
@@ -145,7 +141,13 @@ impl Error {
pub(crate) fn into_response_with_etag(self, etag: Etag) -> Response {
let params = CacheParams::error(etag);
let mut response = self.into_response();
let body = build_error_body(self.status, self.code, self.message);
let mut response = (
self.status,
[(header::CONTENT_TYPE, "application/problem+json")],
body,
)
.into_response();
params.apply_to(response.headers_mut());
response
}
@@ -174,9 +176,7 @@ impl IntoResponse for Error {
body,
)
.into_response();
let h = response.headers_mut();
h.insert_cache_control(CC_ERROR);
h.insert_cdn_cache_control(CC_ERROR);
CacheParams::apply_error_cache_control(response.headers_mut());
response
}
}

View File

@@ -1,10 +0,0 @@
use schemars::JsonSchema;
use serde::Deserialize;
use brk_types::Limit;
#[derive(Deserialize, JsonSchema)]
pub struct LimitParam {
#[serde(default)]
pub limit: Limit,
}

View File

@@ -5,7 +5,6 @@ mod blockhash_param;
mod blockhash_start_index;
mod blockhash_tx_index;
mod height_param;
mod limit_param;
mod pool_slug_param;
mod series_param;
mod time_period_param;
@@ -24,7 +23,6 @@ pub use blockhash_param::*;
pub use blockhash_start_index::*;
pub use blockhash_tx_index::*;
pub use height_param::*;
pub use limit_param::*;
pub use pool_slug_param::*;
pub use series_param::*;
pub use time_period_param::*;