capy-http 0.10.0

Axum routing and JSON response conveniences for Capy.
Documentation
//! Thin Axum routing and response conveniences.

mod contract;
mod extract;
mod pagination;
mod routes;

use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
};
pub use contract::{ApiDocument, ApiRouteInfo};
pub use extract::{Json, Path, Query};
pub use pagination::{Page, PageMeta, Paginated, Pagination, PaginationMeta};
pub use routes::{RouteInfo, Routes};
pub use schemars as schema;
pub use schemars::JsonSchema as ApiSchema;
use serde::Serialize;

/// A JSON response with HTTP 201 Created.
#[derive(Debug, Clone)]
pub struct Created<T>(pub T);

impl<T: Serialize> IntoResponse for Created<T> {
    fn into_response(self) -> Response {
        (StatusCode::CREATED, Json(self.0)).into_response()
    }
}

/// An empty response with HTTP 204 No Content.
#[derive(Debug, Clone, Copy)]
pub struct NoContent;

impl IntoResponse for NoContent {
    fn into_response(self) -> Response {
        StatusCode::NO_CONTENT.into_response()
    }
}

/// The default health response; it deliberately performs no database query.
pub async fn health() -> Json<HealthResponse> {
    Json(HealthResponse { status: "ok" })
}

/// JSON wire data for process liveness.
#[derive(Debug, Serialize, ApiSchema)]
pub struct HealthResponse {
    /// `ok` when the process can serve HTTP.
    pub status: &'static str,
}