#[derive(Debug, Clone)]
pub struct OpenApiConfig {
pub enabled: bool,
pub swagger_ui: bool,
pub serve_spec: bool,
pub swagger_ui_path: String,
pub spec_path: String,
pub visibility: OpenApiVisibility,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OpenApiVisibility {
All,
PublicOnly,
InternalOnly,
}
impl Default for OpenApiConfig {
fn default() -> Self {
Self {
enabled: false,
swagger_ui: false,
serve_spec: false,
swagger_ui_path: "/swagger-ui".to_string(),
spec_path: "/api-docs/openapi.json".to_string(),
visibility: OpenApiVisibility::All,
}
}
}
impl OpenApiConfig {
pub fn from_env() -> Self {
let enabled = std::env::var("OPENAPI_ENABLED")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(false);
let swagger_ui = std::env::var("OPENAPI_SWAGGER_UI")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(false);
let serve_spec = std::env::var("OPENAPI_SERVE_SPEC")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(false);
let visibility = std::env::var("OPENAPI_VISIBILITY")
.ok()
.and_then(|v| match v.to_lowercase().as_str() {
"public" | "public-only" => Some(OpenApiVisibility::PublicOnly),
"internal" | "internal-only" => Some(OpenApiVisibility::InternalOnly),
"all" => Some(OpenApiVisibility::All),
_ => None,
})
.unwrap_or(OpenApiVisibility::All);
Self {
enabled,
swagger_ui,
serve_spec,
visibility,
..Default::default()
}
}
pub fn enable(mut self) -> Self {
self.enabled = true;
self
}
pub fn with_swagger_ui(mut self) -> Self {
self.swagger_ui = true;
self
}
pub fn with_spec(mut self) -> Self {
self.serve_spec = true;
self
}
pub fn visibility(mut self, visibility: OpenApiVisibility) -> Self {
self.visibility = visibility;
self
}
}