Documentation
¶
Overview ¶
Package apidoc 提供接口文档自动生成(对标 Springdoc/Swagger): 注册路由时自动推断文档(函数名/路径模板/动词),支持可选描述覆盖; 启动后输出 OpenAPI 3.0 定义 + 内嵌 API 浏览页面。
用法(约定优先,只写差异):
// ① 一行注册,全自动推断
apidoc.GET(app, "/api/v1/orders/{id}", GetOrder)
// ② 只写自定义部分
apidoc.POST(app, "/api/v1/orders", CreateOrder,
apidoc.Summary("创建订单(自定义)"),
apidoc.Responds(&Order{}),
)
// ③ CRUD 工厂:一次注册 5 个标准接口 + 完整文档
apidoc.CRUD(app, "/api/v1/orders", &Order{})
// ④ 页面与定义
// GET /docs API 浏览页面(内嵌)
// GET /docs/api.json OpenAPI 3.0 定义
Index ¶
- func CRUD(app *iris.Application, path string, model interface{}, ...)
- func DELETE(app *iris.Application, path string, handler iris.Handler, options ...Option)
- func GET(app *iris.Application, path string, handler iris.Handler, options ...Option)
- func Handle(app *iris.Application, method, path string, handler iris.Handler, ...)
- func POST(app *iris.Application, path string, handler iris.Handler, options ...Option)
- func PUT(app *iris.Application, path string, handler iris.Handler, options ...Option)
- func Register(app *iris.Application, prefix string, config Config)
- func ResetStoreForTest()
- type Components
- type Config
- type DocStore
- type Info
- type OpenAPISpec
- type Operation
- type OperationDoc
- type Option
- func Body(model interface{}, required bool, desc string) Option
- func Deprecated() Option
- func Description(text string) Option
- func PathParam(name, paramType string, required bool, desc string) Option
- func QueryParam(name, paramType string, required bool, desc string) Option
- func Respond(code int, desc string, model interface{}) Option
- func Responds(model interface{}) Option
- func Summary(text string) Option
- func Tag(tag string) Option
- type Param
- type ParameterDoc
- type PathItem
- type RequestBody
- type RequestBodyDoc
- type Response
- type ResponseDoc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CRUD ¶
func CRUD(app *iris.Application, path string, model interface{}, listHandler, getHandler, createHandler, updateHandler, deleteHandler iris.Handler)
CRUD 一次注册标准 CRUD 接口并生成文档:
GET <path> 列表(QueryParam page/page_size)
GET <path>/{id} 详情
POST <path> 创建
PUT <path>/{id} 更新
DELETE <path>/{id} 删除
handlers 依次为:List / Get / Create / Update / Delete(均为 iris.Handler)。
func Handle ¶ added in v1.31.0
Handle 注册任意方法路由并收集文档(webiris.Route.Doc 使用)。 与 GET/POST/PUT/DELETE 等价,方法名由调用方指定。
func Register ¶
func Register(app *iris.Application, prefix string, config Config)
Register 注册文档服务:
GET <prefix> API 浏览页面(内嵌,无外部资源) GET <prefix>/api.json OpenAPI 3.0 定义
func ResetStoreForTest ¶ added in v1.31.0
func ResetStoreForTest()
ResetStoreForTest 重置全局文档收集器(测试隔离用)。
Types ¶
type Components ¶
type Components struct {
Schemas map[string]jsonSchema `json:"schemas"`
}
Components 组件(模型 Schema)。
type DocStore ¶
type DocStore struct {
// contains filtered or unexported fields
}
DocStore 文档收集器。
func (*DocStore) RegisterModel ¶
func (s *DocStore) RegisterModel(model interface{})
RegisterModel 注册模型(可选,schema 引用用)。
type Info ¶
type Info struct {
Title string `json:"title"`
Description string `json:"description"`
Version string `json:"version"`
}
Info 文档信息。
type OpenAPISpec ¶
type OpenAPISpec struct {
OpenAPI string `json:"openapi"`
Info Info `json:"info"`
Paths map[string]PathItem `json:"paths"`
Components Components `json:"components"`
}
OpenAPISpec OpenAPI 3.0 文档结构。
func BuildOpenAPI ¶
func BuildOpenAPI(store *DocStore, title, version, description string) *OpenAPISpec
BuildOpenAPI 从 DocStore 生成 OpenAPI 3.0 定义。
type Operation ¶
type Operation struct {
Method string // HTTP 方法(GET/POST/PUT/DELETE)
Path string // 路由路径(如 /api/v1/orders/{id})
Summary string // 摘要
Description string // 详细描述
Tags []string // 标签
Params []Param // 参数
Responses []Response // 响应
Deprecated bool // 弃用标记
RequestBody *RequestBody // 请求体(有 body 参数时)
HandlerName string // 处理函数名(调试)
}
Operation 接口操作元数据(OpenAPI operation 对齐)。
type OperationDoc ¶
type OperationDoc struct {
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Tags []string `json:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Parameters []ParameterDoc `json:"parameters,omitempty"`
RequestBody *RequestBodyDoc `json:"requestBody,omitempty"`
Responses map[string]ResponseDoc `json:"responses"`
Security []map[string][]string `json:"security,omitempty"`
}
OperationDoc 操作文档(OpenAPI 对齐)。
type Option ¶
type Option func(*Operation)
Option 文档描述选项(约定之外的自定义)。
func QueryParam ¶
QueryParam 查询参数。
type Param ¶
type Param struct {
Name string
In string // path / query / header
Type string // string/int/int64/float64/bool
Required bool
Desc string
}
Param 参数定义。
type ParameterDoc ¶
type ParameterDoc struct {
Name string `json:"name"`
In string `json:"in"`
Required bool `json:"required"`
Description string `json:"description,omitempty"`
Schema jsonSchema `json:"schema"`
}
ParameterDoc 参数文档。
type RequestBody ¶
RequestBody 请求体定义。
type RequestBodyDoc ¶
type RequestBodyDoc struct {
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Content map[string]struct {
Schema jsonSchema `json:"schema"`
} `json:"content"`
}
RequestBodyDoc 请求体文档。
type ResponseDoc ¶
type ResponseDoc struct {
Description string `json:"description"`
Content map[string]struct {
Schema jsonSchema `json:"schema"`
} `json:"content,omitempty"`
}
ResponseDoc 响应文档。