// swagger.v - Swagger UI 中间件 // 本模块提供 Swagger UI 文档界面功能,类似于 Hono.js 的 @hono/swagger-ui 中间件 // 支持 OpenAPI 3.0/3.1 规范 module hono import net.http // ============================================================================ // Swagger UI 配置选项 (Task 7.1) // ============================================================================ // SwaggerUIOptions - Swagger UI 配置选项 // 使用示例: // app.get('/ui', hono.swagger_ui(hono.SwaggerUIOptions{ url: '/doc' })) pub struct SwaggerUIOptions { pub mut: url string = '/doc' // OpenAPI 文档 URL title string = 'API Documentation' // 页面标题 deep_linking bool = true // 启用深度链接 display_request_duration bool = true // 显示请求耗时 default_models_expand_depth int = 1 // 模型展开深度 doc_expansion string = 'list' // 文档展开方式: 'list', 'full', 'none' filter bool // 启用过滤 show_extensions bool // 显示扩展 show_common_extensions bool = true // 显示常用扩展 try_it_out_enabled bool = true // 启用 Try it out custom_css string // 自定义 CSS custom_js string // 自定义 JavaScript custom_css_url string // 自定义 CSS URL custom_js_url string // 自定义 JavaScript URL } // ============================================================================ // Swagger UI HTML 生成 (Task 7.2) // ============================================================================ // generate_swagger_html - 生成 Swagger UI HTML 页面 // 使用 CDN 链接加载 Swagger UI 资源 // 应用配置选项到 SwaggerUIBundle 配置 fn generate_swagger_html(options SwaggerUIOptions) string { // 构建自定义 CSS URL 标签 custom_css_url_tag := if options.custom_css_url.len > 0 { '' } else { '' } // 构建自定义 JS URL 标签 custom_js_url_tag := if options.custom_js_url.len > 0 { '' } else { '' } // 构建自定义 CSS 内联样式 custom_css_inline := if options.custom_css.len > 0 { options.custom_css } else { '' } // 构建自定义 JS 内联脚本 custom_js_inline := if options.custom_js.len > 0 { options.custom_js } else { '' } // 转换布尔值为 JavaScript 字符串 deep_linking_str := if options.deep_linking { 'true' } else { 'false' } display_request_duration_str := if options.display_request_duration { 'true' } else { 'false' } filter_str := if options.filter { 'true' } else { 'false' } show_extensions_str := if options.show_extensions { 'true' } else { 'false' } show_common_extensions_str := if options.show_common_extensions { 'true' } else { 'false' } try_it_out_enabled_str := if options.try_it_out_enabled { 'true' } else { 'false' } return '