feat(server): 标准 HTTP 服务器重构 + FastAPI 风格自动文档

This commit is contained in:
snowy-yang committed 2026-08-29 11:51:07 +08:00
1 parent 1dfb7a59ca
commit 59b2aae584
9 files changed
+1236 -254

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
// 文档页面 —— Swagger UI(/docs)与 ReDoc(/redoc),静态 HTML + CDN 资源
const SWAGGER_CDN = 'https://unpkg.com/swagger-ui-dist@5/swagger-ui.css'
const SWAGGER_BUNDLE_CDN = 'https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js'
const REDOC_CDN = 'https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js'
const htmlShell = (title, body) => `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${title}</title>
${body.head}
</head>
<body>
${body.content}
</body>
</html>`
export const swaggerHtml = ({ title, specUrl = '/openapi.json' }) => htmlShell(title, {
head: `
<link rel="stylesheet" href="${SWAGGER_CDN}">
<style>body { margin: 0; }</style>`,
content: `
<div id="swagger-ui"></div>
<script src="${SWAGGER_BUNDLE_CDN}" crossorigin></script>
<script>
window.onload = () => window.ui = SwaggerUIBundle({
url: '${specUrl}',
dom_id: '#swagger-ui',
docExpansion: 'none',
persistAuthorization: true,
tryItOutEnabled: true,
})
</script>`,
})
export const redocHtml = ({ title, specUrl = '/openapi.json' }) => htmlShell(`${title} (ReDoc)`, {
head: `
<style>body { margin: 0; padding: 0; }</style>`,
content: `
<redoc spec-url="${specUrl}" hide-download-button="true"></redoc>
<script src="${REDOC_CDN}"></script>`,
})