use super::types::RustApi;
use crate::middleware::{LayerStack, MiddlewareLayer};
pub struct RustApiConfig {
docs_path: Option<String>,
docs_enabled: bool,
api_title: String,
api_version: String,
api_description: Option<String>,
body_limit: Option<usize>,
layers: LayerStack,
}
impl Default for RustApiConfig {
fn default() -> Self {
Self::new()
}
}
impl RustApiConfig {
pub fn new() -> Self {
Self {
docs_path: Some("/docs".to_string()),
docs_enabled: true,
api_title: "RustAPI".to_string(),
api_version: "1.0.0".to_string(),
api_description: None,
body_limit: None,
layers: LayerStack::new(),
}
}
pub fn docs_path(mut self, path: impl Into<String>) -> Self {
self.docs_path = Some(path.into());
self
}
pub fn docs_enabled(mut self, enabled: bool) -> Self {
self.docs_enabled = enabled;
self
}
pub fn openapi_info(
mut self,
title: impl Into<String>,
version: impl Into<String>,
description: Option<impl Into<String>>,
) -> Self {
self.api_title = title.into();
self.api_version = version.into();
self.api_description = description.map(|d| d.into());
self
}
pub fn body_limit(mut self, limit: usize) -> Self {
self.body_limit = Some(limit);
self
}
pub fn layer<L>(mut self, layer: L) -> Self
where
L: MiddlewareLayer,
{
self.layers.push(Box::new(layer));
self
}
pub fn build(self) -> RustApi {
let mut app = RustApi::new().mount_auto_routes_grouped();
if let Some(limit) = self.body_limit {
app = app.body_limit(limit);
}
app = app.openapi_info(
&self.api_title,
&self.api_version,
self.api_description.as_deref(),
);
#[cfg(feature = "swagger-ui")]
if self.docs_enabled {
if let Some(path) = self.docs_path {
app = app.docs(&path);
}
}
app.layers.extend(self.layers);
app
}
pub async fn run(
self,
addr: impl AsRef<str>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.build().run(addr.as_ref()).await
}
}