使用 Gin-Docs 自动生成 API 文档

该插件移植自 Python 的 Flask-Docs,可以根据代码注释生成文档页面,支持离线文档下载和生成,支持在线调试,支持密码认证。

Gin-Docs

Gin API 文档自动生成插件

特性

  • 根据代码注释自动生成 Markdown 文档
  • 支持离线 Markdown 文档下载
  • 支持在线调试
  • 支持生成离线文档
    • HTML
    • Markdown

链接

https://github.com/kwkwc/gin-docs

安装

go get -u github.com/kwkwc/gin-docs

使用

go 复制代码
import (
    "github.com/gin-gonic/gin"
    gd "github.com/kwkwc/gin-docs"
)

r := gin.Default()
r.POST("/api/todo", AddTodo)
r.GET("/api/todo", GetTodo)

c := &gd.Config{}
apiDoc := gd.ApiDoc{Ge: r, Conf: c.Default()}
apiDoc.OnlineHtml()

r.Run()

查看文档页面

shell 复制代码
http://127.0.0.1/docs/api/

演示

在线演示

配置

go 复制代码
type Config struct {
	// 标题, default `API Doc`
	Title string
	// 版本, default `1.0.0`
	Version string
	// 描述
	Description string

	// 自定义 CDN CSS 模板
	CdnCssTemplate string
	// 自定义 CDN JS 模板
	CdnJsTemplate string

	// 自定义 url prefix, default `/docs/api`
	UrlPrefix string
	// 文档不存在时的描述, default `No documentation found for this API`
	NoDocText string
	// 启用文档页面, default `true`
	Enable bool
	// 使用 CDN, default `false`
	Cdn bool
	// 需要排除的 API 包名
	Exclude []string
	// 允许显示的方法, default `[]string{"GET", "POST", "PUT", "DELETE", "PATCH"}`
	MethodsList []string
	// SHA256 加密的授权密码,例如这里是 admin
	// echo -n admin | shasum -a 256
	// `8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918`
	PasswordSha2 string
	// 启用 markdown 处理所有文档, default `true`
	AllMd bool
}

标记 @@@

shell 复制代码
# 默认以 markdown 处理所有文档
# 1. 如果希望指定处理,请使用 `@@@` 包裹
# 2. 如果希望展示原始文档,请关闭 `Config.AllMd`,并去除 `@@@` 标记

@@@
# 在这里写下你的 markdown 文档
@@@

API

go 复制代码
/*
Add todo

### args
|  args | required | location | type   |  help    |
|-------|----------|----------|--------|----------|
| name  |  true    |  json    | string | todo name |
| type  |  true    |  json    | string | todo type |

### request
```json
{"name": "xx", "type": "code"}
```

### response
```json
{"code": xxxx, "msg": "xxx", "data": null}
```
*/
func AddTodo(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"todo": "post todo",
	})
}
go 复制代码
/*
Get todo

### description
> Get todo

### args
|  args | required | location |  type  |  help    |
|-------|----------|----------|--------|----------|
|  name |  true    |  query   | string | todo name |
|  type |  false   |  query   | string | todo type |

### request
```
http://127.0.0.1:8080/api/todo?name=xxx&type=code
```

### response
```json
{"code": xxxx, "msg": "xxx", "data": null}
```
*/
func GetTodo(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"todo": "get todo",
	})
}


调试器

认证

生成离线文档

go 复制代码
r := gin.Default()

c := &gd.Config{}
apiDoc := gd.ApiDoc{Ge: r, Conf: c.Default()}

// HTML: 在 `htmldoc/` 生成离线 HTML 文档
out := "htmldoc"
apiDoc.OfflineHtml(out, true)

r.StaticFile(c.UrlPrefix+"/", filepath.Join(out, "index.html"))
r.StaticFile(c.UrlPrefix+"/data", filepath.Join(out, "data"))
r.Static(c.UrlPrefix+"/static", filepath.Join(out, "static"))

// Markdown: 生成 `doc.md` 离线 Markdown 文档
apiDoc.OfflineMarkdown("doc.md", true)

示例

Complete example

开发

bash 复制代码
# 克隆代码
git clone git@github.com:kwkwc/gin-docs.git

# 工作目录
cd gin-docs

# 安装依赖
make install

# 运行检查
make check-all

移植项目

Flask-Docs

相关推荐
学逆向的2 分钟前
win32注入代码
开发语言·网络安全·api·win32
谢亮_vipxieliang3 小时前
Go Gin 中间件与参数验证:从入门到实战
中间件·golang·gin
VIP_CQCRE4 小时前
用 Ace Data Cloud 接入 OpenAI 兼容语音转文字:一份能直接运行的 API 指南
python·openai·api·语音识别·acedatacloud
VIP_CQCRE10 小时前
ChatBox 接入 Ace Data Cloud 实战:配置 OpenAI 兼容模型,附 404/401 排错
ai·api·chatbox·ace data cloud
高频因子挖掘机12 小时前
历史 K 线突然少一天?用交易日历、停牌信息和数据校验逐步排查
后端·github·api
Laurie三省12 小时前
现场版却配上录音室版的歌词?聊聊我给开源 Mac 歌词软件写的多源打分算法
算法·macos·go
高频因子挖掘机12 小时前
股票池一大就请求缓慢?量化系统批量获取行情的设计与优化
后端·github·api
高频因子挖掘机15 小时前
量化回测前的数据审计:如何判断股票历史行情是否缺失、重复或存在偏差
后端·github·api
VIP_CQCRE1 天前
Nano Banana 图像 API:角色一致性、修图与商品图,一次接入怎么做?
api·ai绘图·图像生成·nano banana·ace data cloud
newerp1 天前
Execution Trace 深入实战:可视化调度与系统停顿分析
后端·程序员·go