fix(doc): fix doc
This commit is contained in:
106
src/docs.rs
106
src/docs.rs
@@ -5,11 +5,61 @@ use crate::models::{
|
||||
UpdateTenantEnabledAppsRequest, UpdateTenantRequest, UpdateTenantStatusRequest,
|
||||
UpdateUserRequest, UpdateUserRolesRequest, User, UserResponse,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use utoipa::openapi::security::{HttpAuthScheme, HttpBuilder, SecurityScheme};
|
||||
use utoipa::{Modify, OpenApi};
|
||||
|
||||
struct SecurityAddon;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct DocsEnvConfig {
|
||||
default_tenant_id: Option<String>,
|
||||
default_token: Option<String>,
|
||||
}
|
||||
|
||||
impl DocsEnvConfig {
|
||||
fn from_env() -> Self {
|
||||
let require_tenant = std::env::var("IAM_DOCS_REQUIRE_TENANT_ID")
|
||||
.map(|v| v == "1")
|
||||
.unwrap_or(false);
|
||||
let require_token = std::env::var("IAM_DOCS_REQUIRE_TOKEN")
|
||||
.map(|v| v == "1")
|
||||
.unwrap_or(false);
|
||||
|
||||
let default_tenant_id = std::env::var("IAM_DOCS_DEFAULT_TENANT_ID").ok();
|
||||
let default_token = std::env::var("IAM_DOCS_DEFAULT_TOKEN").ok();
|
||||
|
||||
if require_tenant && default_tenant_id.is_none() {
|
||||
panic!("IAM_DOCS_REQUIRE_TENANT_ID=1 but IAM_DOCS_DEFAULT_TENANT_ID is not set");
|
||||
}
|
||||
if require_token && default_token.is_none() {
|
||||
panic!("IAM_DOCS_REQUIRE_TOKEN=1 but IAM_DOCS_DEFAULT_TOKEN is not set");
|
||||
}
|
||||
|
||||
Self {
|
||||
default_tenant_id,
|
||||
default_token,
|
||||
}
|
||||
}
|
||||
|
||||
fn tenant_example(&self) -> String {
|
||||
self.default_tenant_id
|
||||
.clone()
|
||||
.unwrap_or_else(|| "11111111-1111-1111-1111-111111111111".to_string())
|
||||
}
|
||||
|
||||
fn authorization_example(&self) -> String {
|
||||
let Some(token) = self.default_token.clone() else {
|
||||
return "Bearer <access_token>".to_string();
|
||||
};
|
||||
if token.to_ascii_lowercase().starts_with("bearer ") {
|
||||
token
|
||||
} else {
|
||||
format!("Bearer {}", token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Modify for SecurityAddon {
|
||||
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
|
||||
let components = openapi
|
||||
@@ -24,6 +74,51 @@ impl Modify for SecurityAddon {
|
||||
.build(),
|
||||
),
|
||||
);
|
||||
|
||||
let cfg = DocsEnvConfig::from_env();
|
||||
apply_header_parameter_examples(openapi, &cfg);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_header_parameter_examples(openapi: &mut utoipa::openapi::OpenApi, cfg: &DocsEnvConfig) {
|
||||
use utoipa::openapi::path::ParameterBuilder;
|
||||
use utoipa::openapi::path::ParameterIn;
|
||||
|
||||
let tenant_value = cfg.tenant_example();
|
||||
let auth_value = cfg.authorization_example();
|
||||
|
||||
for (_path, item) in openapi.paths.paths.iter_mut() {
|
||||
let operations = [
|
||||
item.get.as_mut(),
|
||||
item.post.as_mut(),
|
||||
item.put.as_mut(),
|
||||
item.patch.as_mut(),
|
||||
item.delete.as_mut(),
|
||||
];
|
||||
|
||||
for op in operations.into_iter().flatten() {
|
||||
let Some(params) = op.parameters.as_mut() else {
|
||||
continue;
|
||||
};
|
||||
for param in params.iter_mut() {
|
||||
if param.parameter_in == ParameterIn::Header
|
||||
&& param.name.eq_ignore_ascii_case("X-Tenant-ID")
|
||||
{
|
||||
let builder: ParameterBuilder = std::mem::take(param).into();
|
||||
*param = builder
|
||||
.example(Some(Value::String(tenant_value.clone())))
|
||||
.build();
|
||||
}
|
||||
if param.parameter_in == ParameterIn::Header
|
||||
&& param.name.eq_ignore_ascii_case("Authorization")
|
||||
{
|
||||
let builder: ParameterBuilder = std::mem::take(param).into();
|
||||
*param = builder
|
||||
.example(Some(Value::String(auth_value.clone())))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,16 +130,7 @@ impl Modify for SecurityAddon {
|
||||
version = "0.1.0",
|
||||
description = include_str!("../docs/SCALAR_GUIDE.md")
|
||||
),
|
||||
servers(
|
||||
(
|
||||
url = "https://{env}/api",
|
||||
description = "Environment server",
|
||||
variables(
|
||||
("env" = (default = "dev", enum_values("dev", "staging", "prod"))),
|
||||
("port" = (default = "5010"))
|
||||
)
|
||||
)
|
||||
),
|
||||
|
||||
paths(
|
||||
handlers::auth::register_handler,
|
||||
handlers::auth::login_handler,
|
||||
|
||||
Reference in New Issue
Block a user