Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

brokkr-broker::api Rust

Functions

brokkr-broker::api::configure_api_routes

pub

#![allow(unused)]
fn main() {
fn configure_api_routes (dal : DAL , cors_config : & Cors , reloadable_config : Option < ReloadableConfig > ,) -> Router < DAL >
}

Configures and returns the main application router with all API routes

This function is responsible for setting up the entire API structure of the application. It merges routes from all submodules and adds a health check endpoint.

Parameters:

NameTypeDescription
dal-An instance of the Data Access Layer
cors_config-CORS configuration settings
reloadable_config-Optional reloadable configuration for hot-reload support

Returns:

Returns a configured Router instance that includes all API routes and middleware.

Source
#![allow(unused)]
fn main() {
pub fn configure_api_routes(
    dal: DAL,
    cors_config: &Cors,
    reloadable_config: Option<ReloadableConfig>,
) -> Router<DAL> {
    // Build a permissive CORS layer for health/metrics endpoints
    let root_cors = CorsLayer::new()
        .allow_origin(Any)
        .allow_methods(Any)
        .allow_headers(Any);

    Router::new()
        .merge(v1::routes(dal.clone(), cors_config, reloadable_config))
        .route("/healthz", get(healthz))
        .route("/readyz", get(readyz))
        .route("/metrics", get(metrics_handler))
        .layer(root_cors)
        .layer(middleware::from_fn(metrics_middleware))
        .layer(
            TraceLayer::new_for_http()
                .make_span_with(|request: &hyper::Request<_>| {
                    tracing::span!(
                        Level::INFO,
                        "http_request",
                        method = %request.method(),
                        uri = %request.uri(),
                        version = ?request.version(),
                    )
                })
                .on_response(
                    |response: &hyper::Response<_>,
                     latency: std::time::Duration,
                     _span: &tracing::Span| {
                        tracing::info!(
                            status = %response.status(),
                            latency_ms = latency.as_millis(),
                            "response"
                        );
                    },
                ),
        )
}
}

brokkr-broker::api::healthz

private

#![allow(unused)]
fn main() {
async fn healthz () -> impl IntoResponse
}

Health check endpoint handler

This handler responds to GET requests at the “/healthz” endpoint. It’s used to verify that the API is up and running.

Returns:

Returns a 200 OK status code with “OK” in the body.

Source
#![allow(unused)]
fn main() {
async fn healthz() -> impl IntoResponse {
    (StatusCode::OK, "OK")
}
}

brokkr-broker::api::readyz

private

#![allow(unused)]
fn main() {
async fn readyz () -> impl IntoResponse
}

Ready check endpoint handler

This handler responds to GET requests at the “/readyz” endpoint. It’s used to verify that the API is ready for use.

Returns:

Returns a 200 OK status code with “Ready” in the body.

Source
#![allow(unused)]
fn main() {
async fn readyz() -> impl IntoResponse {
    (StatusCode::OK, "Ready")
}
}

brokkr-broker::api::metrics_handler

private

#![allow(unused)]
fn main() {
async fn metrics_handler () -> impl IntoResponse
}

Metrics endpoint handler

This handler responds to GET requests at the “/metrics” endpoint. It’s used to provide Prometheus metrics about the broker’s operation.

Returns:

Returns a 200 OK status code with Prometheus metrics in text format.

Source
#![allow(unused)]
fn main() {
async fn metrics_handler() -> impl IntoResponse {
    let metrics_data = metrics::encode_metrics();
    (
        StatusCode::OK,
        [("Content-Type", "text/plain; version=0.0.4")],
        metrics_data,
    )
}
}

brokkr-broker::api::metrics_middleware

private

#![allow(unused)]
fn main() {
async fn metrics_middleware (request : Request < Body > , next : Next) -> Response
}

Middleware to record HTTP request metrics

Records request count and duration for each HTTP request.

Source
#![allow(unused)]
fn main() {
async fn metrics_middleware(request: Request<Body>, next: Next) -> Response {
    let start = Instant::now();
    let method = request.method().to_string();
    let path = request.uri().path().to_string();

    // Process the request
    let response = next.run(request).await;

    // Record metrics (skip the /metrics endpoint itself to avoid recursion)
    if path != "/metrics" {
        let duration = start.elapsed().as_secs_f64();
        let status = response.status().as_u16();
        metrics::record_http_request(&path, &method, status, duration);
    }

    response
}
}