implement capabilities reporting

This commit is contained in:
boring_nick 2023-09-03 21:24:32 +03:00
parent 26084111f7
commit b7a6bfbf7b
1 changed files with 23 additions and 1 deletions

View File

@ -15,7 +15,12 @@ use aide::{
openapi::OpenApi,
redoc::Redoc,
};
use axum::{middleware, response::IntoResponse, Extension, Json, ServiceExt};
use axum::{
http::Request,
middleware::{self, Next},
response::{IntoResponse, Response},
Extension, Json, ServiceExt,
};
use axum_prometheus::PrometheusMetricLayerBuilder;
use prometheus::TextEncoder;
use std::{
@ -30,6 +35,8 @@ use tower_http::{
};
use tracing::{debug, info};
const CAPABILITIES: &[&str] = &["arbitrary-range-query"];
pub async fn run(app: App, mut shutdown_rx: ShutdownRx, bot_tx: Sender<BotMessage>) {
aide::gen::on_error(|error| {
panic!("Could not generate docs: {error}");
@ -131,10 +138,12 @@ pub async fn run(app: App, mut shutdown_rx: ShutdownRx, bot_tx: Sender<BotMessag
}),
)
.api_route("/optout", post(handlers::optout))
.api_route("/capabilities", get(capabilities))
.route("/docs", Redoc::new("/openapi.json").axum_route())
.route("/openapi.json", get(serve_openapi))
.route("/assets/*asset", get(frontend::static_asset))
.fallback(frontend::static_asset)
.layer(middleware::from_fn(capabilities_header_middleware))
.layer(
TraceLayer::new_for_http()
.make_span_with(trace_layer::make_span_with)
@ -173,6 +182,19 @@ pub fn parse_listen_addr(addr: &str) -> Result<SocketAddr, AddrParseError> {
}
}
async fn capabilities() -> Json<Vec<&'static str>> {
Json(CAPABILITIES.to_vec())
}
async fn capabilities_header_middleware<B>(request: Request<B>, next: Next<B>) -> Response {
let mut response = next.run(request).await;
response.headers_mut().insert(
"x-rustlog-capabilities",
CAPABILITIES.join(",").try_into().unwrap(),
);
response
}
async fn metrics() -> impl IntoApiResponse {
let metric_families = prometheus::gather();