Documentation
¶
Overview ¶
Package openapi 提供 OpenAPI 文档生成功能,用于 enhance 框架。
该模块自动从控制器注解生成 OpenAPI 3.0 规范文档,支持 Swagger UI 集成。 参考 SpringDoc OpenAPI 的设计理念。
架构设计 ¶
- OpenAPI: OpenAPI 文档结构,定义 API 规范
- Operation: 操作定义,描述单个 API 端点
- Schema: 数据模型定义,描述请求和响应结构
- SwaggerUI: Swagger UI 集成,提供可视化文档界面
核心功能 ¶
- 文档生成: 自动从代码注解生成 OpenAPI 3.0 文档
- Swagger UI: 集成 Swagger UI,提供交互式 API 文档
- 注解支持: 支持 @Operation、@Parameter、@Response 等注解
- 数据模型: 自动生成请求和响应的数据模型定义
使用方式 ¶
在控制器中使用注解:
// @Operation(summary: "Get user by ID")
// @Parameter(name: "id", in: "path", required: true)
// @Response(code: 200, description: "Success")
func GetUser(c *gin.Context) {
// 处理逻辑
}
启用 Swagger UI:
import _ "github.com/xudefa/enhance/openapi"
访问文档:
浏览器访问: http://localhost:8080/swagger/index.html
配置属性 ¶
- openapi.enabled: 是否启用 OpenAPI 文档(默认 true)
- openapi.title: API 文档标题
- openapi.version: API 版本
- openapi.description: API 描述
配置示例 ¶
环境变量:
export OPENAPI_ENABLED=true export OPENAPI_TITLE="My API" export OPENAPI_VERSION="1.0.0"
Index ¶
- func ServeSwaggerUI(doc *DocumentBuilder, basePath string, port int) error
- type APIOperation
- type APIParam
- type APIResponse
- type APISecurity
- type APITag
- type ComponentsObject
- type ContactObject
- type DocumentBuilder
- func (b *DocumentBuilder) AddPath(path string, method string, operation OperationObject) *DocumentBuilder
- func (b *DocumentBuilder) AddSchema(name string, schema SchemaObject) *DocumentBuilder
- func (b *DocumentBuilder) AddSecurityScheme(name string, scheme SecuritySchemeObject) *DocumentBuilder
- func (b *DocumentBuilder) AddServer(url, description string) *DocumentBuilder
- func (b *DocumentBuilder) AddTag(name, description string) *DocumentBuilder
- func (b *DocumentBuilder) Build() *OpenAPIDocument
- func (b *DocumentBuilder) RegisterController(controller any) *DocumentBuilder
- func (b *DocumentBuilder) RegisterSchema(name string, typ reflect.Type) *DocumentBuilder
- func (b *DocumentBuilder) SaveToFile(path string) error
- func (b *DocumentBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (b *DocumentBuilder) SetContact(name, url, email string) *DocumentBuilder
- func (b *DocumentBuilder) SetInfo(title, version, description string) *DocumentBuilder
- func (b *DocumentBuilder) SetLicense(name, url string) *DocumentBuilder
- func (b *DocumentBuilder) SetTermsOfService(terms string) *DocumentBuilder
- func (b *DocumentBuilder) ToJSON() (string, error)
- func (b *DocumentBuilder) ToJSONBytes() ([]byte, error)
- type ExampleObject
- type HeaderObject
- type InfoObject
- type LicenseObject
- type LinkObject
- type MediaTypeObject
- type OAuthFlowObject
- type OAuthFlowsObject
- type OpenAPIDocument
- type OperationObject
- type ParameterObject
- type PathItem
- type RequestBodyObject
- type ResponseObject
- type SchemaObject
- type SecuritySchemeObject
- type ServerObject
- type ServerVariable
- type TagObject
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ServeSwaggerUI ¶
func ServeSwaggerUI(doc *DocumentBuilder, basePath string, port int) error
ServeSwaggerUI 提供 Swagger UI 服务
Types ¶
type APIOperation ¶
type APIOperation struct {
// Summary 操作摘要
Summary string
// Description 操作描述
Description string
// OperationID 操作 ID
OperationID string
// Tags 标签列表
Tags []string
// Deprecated 是否已弃用
Deprecated bool
}
APIOperation API 操作注解
type APIParam ¶
type APIParam struct {
// Name 参数名称
Name string
// In 参数位置 (query, path, header, cookie)
In string
// Description 参数描述
Description string
// Required 是否必填
Required bool
// Example 示例值
Example any
}
APIParam API 参数注解
type APIResponse ¶
type APIResponse struct {
// StatusCode HTTP 状态码
StatusCode int
// Description 响应描述
Description string
// Type 响应类型
Type any
}
APIResponse API 响应注解
type APISecurity ¶
APISecurity API 安全注解
type ComponentsObject ¶
type ComponentsObject struct {
Schemas map[string]SchemaObject `json:"schemas,omitempty"`
Responses map[string]ResponseObject `json:"responses,omitempty"`
Parameters map[string]ParameterObject `json:"parameters,omitempty"`
RequestBodies map[string]RequestBodyObject `json:"requestBodies,omitempty"`
SecuritySchemes map[string]SecuritySchemeObject `json:"securitySchemes,omitempty"`
}
ComponentsObject 组件对象
type ContactObject ¶
type ContactObject struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}
ContactObject 联系信息
type DocumentBuilder ¶
type DocumentBuilder struct {
// contains filtered or unexported fields
}
DocumentBuilder 文档构建器
func (*DocumentBuilder) AddPath ¶
func (b *DocumentBuilder) AddPath(path string, method string, operation OperationObject) *DocumentBuilder
AddPath 手动添加路径
func (*DocumentBuilder) AddSchema ¶
func (b *DocumentBuilder) AddSchema(name string, schema SchemaObject) *DocumentBuilder
AddSchema 添加 Schema
func (*DocumentBuilder) AddSecurityScheme ¶
func (b *DocumentBuilder) AddSecurityScheme(name string, scheme SecuritySchemeObject) *DocumentBuilder
AddSecurityScheme 添加安全方案
func (*DocumentBuilder) AddServer ¶
func (b *DocumentBuilder) AddServer(url, description string) *DocumentBuilder
AddServer 添加服务器
func (*DocumentBuilder) AddTag ¶
func (b *DocumentBuilder) AddTag(name, description string) *DocumentBuilder
AddTag 添加标签
func (*DocumentBuilder) RegisterController ¶
func (b *DocumentBuilder) RegisterController(controller any) *DocumentBuilder
RegisterController 注册控制器
func (*DocumentBuilder) RegisterSchema ¶
func (b *DocumentBuilder) RegisterSchema(name string, typ reflect.Type) *DocumentBuilder
RegisterSchema 注册结构体 Schema
func (*DocumentBuilder) SaveToFile ¶
func (b *DocumentBuilder) SaveToFile(path string) error
SaveToFile 保存到文件
func (*DocumentBuilder) ServeHTTP ¶
func (b *DocumentBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP 实现 http.Handler,提供 OpenAPI JSON 端点
func (*DocumentBuilder) SetContact ¶
func (b *DocumentBuilder) SetContact(name, url, email string) *DocumentBuilder
SetContact 设置联系信息
func (*DocumentBuilder) SetInfo ¶
func (b *DocumentBuilder) SetInfo(title, version, description string) *DocumentBuilder
SetInfo 设置文档信息
func (*DocumentBuilder) SetLicense ¶
func (b *DocumentBuilder) SetLicense(name, url string) *DocumentBuilder
SetLicense 设置许可证信息
func (*DocumentBuilder) SetTermsOfService ¶
func (b *DocumentBuilder) SetTermsOfService(terms string) *DocumentBuilder
SetTermsOfService 设置服务条款
func (*DocumentBuilder) ToJSONBytes ¶
func (b *DocumentBuilder) ToJSONBytes() ([]byte, error)
ToJSONBytes 转换为 JSON 字节
type ExampleObject ¶
type ExampleObject struct {
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Value any `json:"value,omitempty"`
ExternalValue string `json:"externalValue,omitempty"`
}
ExampleObject 示例对象
type HeaderObject ¶
type HeaderObject struct {
Description string `json:"description,omitempty"`
Schema *SchemaObject `json:"schema,omitempty"`
}
HeaderObject 头部对象
type InfoObject ¶
type InfoObject struct {
Title string `json:"title"`
Version string `json:"version"`
Description string `json:"description,omitempty"`
TermsOfService string `json:"termsOfService,omitempty"`
Contact *ContactObject `json:"contact,omitempty"`
License *LicenseObject `json:"license,omitempty"`
}
InfoObject 文档信息
type LicenseObject ¶
LicenseObject 许可证信息
type LinkObject ¶
type LinkObject struct {
OperationRef string `json:"operationRef,omitempty"`
OperationID string `json:"operationId,omitempty"`
Parameters map[string]string `json:"parameters,omitempty"`
}
LinkObject 链接对象
type MediaTypeObject ¶
type MediaTypeObject struct {
Schema *SchemaObject `json:"schema,omitempty"`
Example any `json:"example,omitempty"`
Examples map[string]ExampleObject `json:"examples,omitempty"`
}
MediaTypeObject 媒体类型对象
type OAuthFlowObject ¶
type OAuthFlowObject struct {
AuthorizationURL string `json:"authorizationUrl,omitempty"`
TokenURL string `json:"tokenUrl,omitempty"`
RefreshURL string `json:"refreshUrl,omitempty"`
Scopes map[string]string `json:"scopes"`
}
OAuthFlowObject OAuth 流程对象
type OAuthFlowsObject ¶
type OAuthFlowsObject struct {
Implicit *OAuthFlowObject `json:"implicit,omitempty"`
Password *OAuthFlowObject `json:"password,omitempty"`
ClientCredentials *OAuthFlowObject `json:"clientCredentials,omitempty"`
AuthorizationCode *OAuthFlowObject `json:"authorizationCode,omitempty"`
}
OAuthFlowsObject OAuth 流程对象
type OpenAPIDocument ¶
type OpenAPIDocument struct {
OpenAPI string `json:"openapi"`
Info InfoObject `json:"info"`
Servers []ServerObject `json:"servers,omitempty"`
Paths map[string]PathItem `json:"paths"`
Components *ComponentsObject `json:"components,omitempty"`
Tags []TagObject `json:"tags,omitempty"`
}
OpenAPIDocument OpenAPI 3.0 文档
type OperationObject ¶
type OperationObject struct {
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
OperationID string `json:"operationId,omitempty"`
Tags []string `json:"tags,omitempty"`
Parameters []ParameterObject `json:"parameters,omitempty"`
RequestBody *RequestBodyObject `json:"requestBody,omitempty"`
Responses map[string]ResponseObject `json:"responses"`
Deprecated bool `json:"deprecated,omitempty"`
Security []map[string][]string `json:"security,omitempty"`
}
OperationObject 操作对象
type ParameterObject ¶
type ParameterObject struct {
Name string `json:"name"`
In string `json:"in"` // query, path, header, cookie
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Schema *SchemaObject `json:"schema,omitempty"`
Example any `json:"example,omitempty"`
}
ParameterObject 参数对象
type PathItem ¶
type PathItem struct {
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Get *OperationObject `json:"get,omitempty"`
Post *OperationObject `json:"post,omitempty"`
Put *OperationObject `json:"put,omitempty"`
Delete *OperationObject `json:"delete,omitempty"`
Patch *OperationObject `json:"patch,omitempty"`
Parameters []ParameterObject `json:"parameters,omitempty"`
Tags []string `json:"tags,omitempty"`
}
PathItem 路径项
type RequestBodyObject ¶
type RequestBodyObject struct {
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Content map[string]MediaTypeObject `json:"content"`
}
RequestBodyObject 请求体对象
type ResponseObject ¶
type ResponseObject struct {
Description string `json:"description"`
Headers map[string]HeaderObject `json:"headers,omitempty"`
Content map[string]MediaTypeObject `json:"content,omitempty"`
Links map[string]LinkObject `json:"links,omitempty"`
}
ResponseObject 响应对象
type SchemaObject ¶
type SchemaObject struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Description string `json:"description,omitempty"`
Properties map[string]SchemaObject `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
Items *SchemaObject `json:"items,omitempty"`
AdditionalProperties *SchemaObject `json:"additionalProperties,omitempty"`
Enum []string `json:"enum,omitempty"`
Default any `json:"default,omitempty"`
Example any `json:"example,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
MinLength *int `json:"minLength,omitempty"`
MaxLength *int `json:"maxLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
Nullable bool `json:"nullable,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
WriteOnly bool `json:"writeOnly,omitempty"`
}
SchemaObject Schema 对象
type SecuritySchemeObject ¶
type SecuritySchemeObject struct {
Type string `json:"type"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
In string `json:"in,omitempty"`
Scheme string `json:"scheme,omitempty"`
BearerFormat string `json:"bearerFormat,omitempty"`
Flows *OAuthFlowsObject `json:"flows,omitempty"`
OpenIDConnectURL string `json:"openIdConnectUrl,omitempty"`
}
SecuritySchemeObject 安全方案对象
type ServerObject ¶
type ServerObject struct {
URL string `json:"url"`
Description string `json:"description,omitempty"`
Variables map[string]ServerVariable `json:"variables,omitempty"`
}
ServerObject 服务器信息
type ServerVariable ¶
type ServerVariable struct {
Default string `json:"default"`
Description string `json:"description,omitempty"`
Enum []string `json:"enum,omitempty"`
}
ServerVariable 服务器变量