使用 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

相关推荐
Mgx1 小时前
从“CPU 烧开水“到优雅暂停:Go 里 sync.Cond 的正确打开方式
go
GM_82812 小时前
从0开始在Go当中使用Apache Thrift框架(万字讲解+图文教程+详细代码)
rpc·go·apache·thrift
記億揺晃着的那天15 小时前
API设计中的幂等性详解
api·后端开发·幂等性
Kratos开源社区18 小时前
别卷 LangChain 了!Blades AI 框架让 Go 开发者轻松打造智能体
go·agent·ai编程
Kratos开源社区18 小时前
跟 Blades 学 Agent 设计 - 01 用“提示词链”让你的 AI 助手变身超级特工
llm·go·agent
百锦再1 天前
第10章 错误处理
java·git·ai·rust·go·错误·pathon
Mgx2 天前
从 4.8 秒到 0.25 秒:我是如何把 Go 正则匹配提速 19 倍的?
go
遥天棋子3 天前
实战PaddleOCR自动识别车位坐标并渲染可点击按钮
go
久违 °3 天前
【安全开发】Nuclei源码分析-任务执行流程(三)
安全·网络安全·go
喵个咪3 天前
开箱即用的GO后台管理系统 Kratos Admin - 数据脱敏和隐私保护
后端·go·protobuf