footing 0.6.6

Feature-gated foundations for Rust backend services
Documentation
//! Export a documented Axum API: `cargo run --example openapi --features openapi,http-stack`.

use axum::{Json, Router};
use footing::{
    http::{
        ApiProblem, CursorPagination, EndpointPolicy, HttpMiddlewareConfig, HttpMiddlewareStack,
        openapi::{OpenApiRouter, route, utoipa},
    },
    pagination::{CursorPage, PageLimits},
};
use utoipa::OpenApi;

#[derive(serde::Serialize, utoipa::ToSchema)]
struct Item {
    /// Stable item identifier.
    id: u64,
}

/// List inventory items.
#[utoipa::path(
    get, path = "/items", params(CursorPagination),
    responses(
        (status = 200, description = "Page of inventory items", body = CursorPage<Item>),
        (status = 400, response = inline(ApiProblem))
    )
)]
async fn list_items(page: CursorPagination) -> Result<Json<CursorPage<Item>>, ApiProblem> {
    // An application would load page.limit_with_extra() items and build its next cursor.
    CursorPage::new(vec![], page.page_size(), None, None)
        .map(Json)
        .map_err(|_| ApiProblem::internal_error())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    #[derive(utoipa::OpenApi)]
    #[openapi(info(title = "Inventory API", version = "1.0.0"))]
    struct Api;

    let stack = HttpMiddlewareStack::builder(HttpMiddlewareConfig::standard()).build()?;
    let (router, document) = OpenApiRouter::new(Api::openapi())?
        .route(
            route!(list_items)?
                .with_cursor_pagination(PageLimits::new(20, 50)?)?
                .with_contract(|_| EndpointPolicy::new())?,
        )?
        .into_parts_with_stack(stack)?;
    // Export the specification; an application can also serve it with Json(document)
    // and mount its chosen documentation UI after configuring access control.
    println!("{}", document.to_pretty_json()?);
    let _: Router = router;
    Ok(())
}