Skip to main content

capy_http/
lib.rs

1//! Thin Axum routing and response conveniences.
2
3mod contract;
4mod extract;
5mod pagination;
6mod routes;
7
8use axum::{
9    http::StatusCode,
10    response::{IntoResponse, Response},
11};
12pub use contract::{ApiDocument, ApiRouteInfo};
13pub use extract::{Json, Path, Query};
14pub use pagination::{Page, PageMeta, Paginated, Pagination, PaginationMeta};
15pub use routes::{RouteInfo, Routes};
16pub use schemars as schema;
17pub use schemars::JsonSchema as ApiSchema;
18use serde::Serialize;
19
20/// A JSON response with HTTP 201 Created.
21#[derive(Debug, Clone)]
22pub struct Created<T>(pub T);
23
24impl<T: Serialize> IntoResponse for Created<T> {
25    fn into_response(self) -> Response {
26        (StatusCode::CREATED, Json(self.0)).into_response()
27    }
28}
29
30/// An empty response with HTTP 204 No Content.
31#[derive(Debug, Clone, Copy)]
32pub struct NoContent;
33
34impl IntoResponse for NoContent {
35    fn into_response(self) -> Response {
36        StatusCode::NO_CONTENT.into_response()
37    }
38}
39
40/// The default health response; it deliberately performs no database query.
41pub async fn health() -> Json<HealthResponse> {
42    Json(HealthResponse { status: "ok" })
43}
44
45/// JSON wire data for process liveness.
46#[derive(Debug, Serialize, ApiSchema)]
47pub struct HealthResponse {
48    /// `ok` when the process can serve HTTP.
49    pub status: &'static str,
50}