extern crate self as rustapi_openapi;
mod config;
#[cfg(feature = "redoc")]
mod redoc;
pub mod schema;
mod schemas;
mod spec;
#[cfg(feature = "swagger-ui")]
mod swagger;
#[cfg(test)]
mod tests;
pub mod versioning;
pub use config::OpenApiConfig;
pub use schemas::{
ErrorBodySchema, ErrorSchema, FieldErrorSchema, ValidationErrorBodySchema,
ValidationErrorSchema,
};
pub use spec::{
ApiInfo, MediaType, OpenApiSpec, Operation, OperationModifier, Parameter, PathItem,
RequestBody, ResponseModifier, ResponseSpec, SchemaRef,
};
pub use rustapi_macros::Schema;
use bytes::Bytes;
use http::{header, Response, StatusCode};
use http_body_util::Full;
pub fn openapi_json(spec: &OpenApiSpec) -> Response<Full<Bytes>> {
match serde_json::to_string_pretty(&spec.to_json()) {
Ok(json) => Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.body(Full::new(Bytes::from(json)))
.unwrap(),
Err(_) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Full::new(Bytes::from("Failed to serialize OpenAPI spec")))
.unwrap(),
}
}
#[cfg(feature = "swagger-ui")]
pub fn swagger_ui_html(openapi_url: &str) -> Response<Full<Bytes>> {
let html = swagger::generate_swagger_html(openapi_url);
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(Full::new(Bytes::from(html)))
.unwrap()
}
#[cfg(feature = "redoc")]
pub fn redoc_html(openapi_url: &str) -> Response<Full<Bytes>> {
let html = redoc::generate_redoc_html(openapi_url, None);
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(Full::new(Bytes::from(html)))
.unwrap()
}
#[cfg(feature = "redoc")]
pub fn redoc_html_with_config(
openapi_url: &str,
title: Option<&str>,
config: &redoc::RedocConfig,
) -> Response<Full<Bytes>> {
let html = redoc::generate_redoc_html_with_config(openapi_url, title, config);
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(Full::new(Bytes::from(html)))
.unwrap()
}
#[cfg(feature = "redoc")]
pub use redoc::{RedocConfig, RedocTheme};