apidoc

package
v1.45.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 7 Imported by: 0

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

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 DELETE

func DELETE(app *iris.Application, path string, handler iris.Handler, options ...Option)

DELETE 注册 DELETE 路由并收集文档。

func GET

func GET(app *iris.Application, path string, handler iris.Handler, options ...Option)

GET 注册 GET 路由并收集文档(自动推断:函数名→摘要、路径模板→参数)。

func Handle added in v1.31.0

func Handle(app *iris.Application, method, path string, handler iris.Handler, options ...Option)

Handle 注册任意方法路由并收集文档(webiris.Route.Doc 使用)。 与 GET/POST/PUT/DELETE 等价,方法名由调用方指定。

func POST

func POST(app *iris.Application, path string, handler iris.Handler, options ...Option)

POST 注册 POST 路由并收集文档。

func PUT

func PUT(app *iris.Application, path string, handler iris.Handler, options ...Option)

PUT 注册 PUT 路由并收集文档。

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 Config

type Config struct {
	Title       string
	Version     string
	Description string
}

Config 文档服务配置。

type DocStore

type DocStore struct {
	// contains filtered or unexported fields
}

DocStore 文档收集器。

func NewDocStore

func NewDocStore() *DocStore

NewDocStore 创建文档收集器。

func Store

func Store() *DocStore

Store 返回全局文档收集器。

func (*DocStore) Add

func (s *DocStore) Add(operation Operation)

Add 注册一条操作。

func (*DocStore) Models

func (s *DocStore) Models() map[string]interface{}

Models 返回已注册模型。

func (*DocStore) Operations

func (s *DocStore) Operations() []Operation

Operations 返回全部操作。

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 定义。

func (*OpenAPISpec) ToJSON

func (s *OpenAPISpec) ToJSON() ([]byte, error)

ToJSON 输出 OpenAPI JSON。

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 Body

func Body(model interface{}, required bool, desc string) Option

Body 请求体模型。

func Deprecated

func Deprecated() Option

Deprecated 标记弃用。

func Description

func Description(text string) Option

Description 详细描述。

func PathParam

func PathParam(name, paramType string, required bool, desc string) Option

PathParam 路径参数(路由模板已自动解析,自定义描述用)。

func QueryParam

func QueryParam(name, paramType string, required bool, desc string) Option

QueryParam 查询参数。

func Respond

func Respond(code int, desc string, model interface{}) Option

Respond 指定状态码响应。

func Responds

func Responds(model interface{}) Option

Responds 成功响应模型(默认 200 通用响应)。

func Summary

func Summary(text string) Option

Summary 自定义摘要。

func Tag

func Tag(tag string) Option

Tag 分组标签(可多次)。

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 PathItem

type PathItem map[string]OperationDoc

PathItem 路径项。

type RequestBody

type RequestBody struct {
	Model    interface{}
	Required bool
	Desc     string
}

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 Response

type Response struct {
	Code  int
	Desc  string
	Model interface{} // 响应 data 模型(可选;nil=通用响应)
}

Response 响应定义。

type ResponseDoc

type ResponseDoc struct {
	Description string `json:"description"`
	Content     map[string]struct {
		Schema jsonSchema `json:"schema"`
	} `json:"content,omitempty"`
}

ResponseDoc 响应文档。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL