feat: 加入带有详细类型注释的 swagger 文档 (#257)

This commit is contained in:
ᴀᴍᴛᴏᴀᴇʀ
2025-02-18 01:55:54 +08:00
committed by GitHub
parent 1467c262a1
commit c995b3bf72
10 changed files with 293 additions and 102 deletions

View File

@@ -3,11 +3,13 @@ use axum::http::HeaderMap;
use axum::middleware::Next;
use axum::response::Response;
use reqwest::StatusCode;
use utoipa::openapi::security::{ApiKey, ApiKeyValue, SecurityScheme};
use utoipa::Modify;
use crate::config::CONFIG;
pub async fn auth(headers: HeaderMap, request: Request, next: Next) -> Result<Response, StatusCode> {
if request.uri().path().starts_with("/api") && get_token(&headers) != CONFIG.auth_token {
if request.uri().path().starts_with("/api/") && get_token(&headers) != CONFIG.auth_token {
return Err(StatusCode::UNAUTHORIZED);
}
Ok(next.run(request).await)
@@ -19,3 +21,19 @@ fn get_token(headers: &HeaderMap) -> Option<String> {
.and_then(|v| v.to_str().ok())
.map(Into::into)
}
pub struct OpenAPIAuth;
impl Modify for OpenAPIAuth {
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
if let Some(schema) = openapi.components.as_mut() {
schema.add_security_scheme(
"Token",
SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::with_description(
"Authorization",
"与配置文件中的 auth_token 相同",
))),
);
}
}
}