feat: 接入新版本openapi文档, 页面渲染兼容性待调试
This commit is contained in:
104
router/server.go
104
router/server.go
@@ -16,19 +16,19 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
apiDoc "git.zhangdeman.cn/zhangdeman/api-doc"
|
||||
apiDocDefine "git.zhangdeman.cn/zhangdeman/api-doc/define"
|
||||
"git.zhangdeman.cn/zhangdeman/api-doc/enums"
|
||||
"git.zhangdeman.cn/zhangdeman/api-doc/openapi"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/define"
|
||||
"git.zhangdeman.cn/zhangdeman/graceful"
|
||||
"github.com/jedib0t/go-pretty/v6/table"
|
||||
"github.com/jedib0t/go-pretty/v6/text"
|
||||
|
||||
apiDoc "git.zhangdeman.cn/zhangdeman/api-doc"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/middleware"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/middleware/request_cors"
|
||||
"github.com/gin-contrib/pprof"
|
||||
|
||||
apiDocDefine "git.zhangdeman.cn/zhangdeman/api-doc/define"
|
||||
apiDocEnum "git.zhangdeman.cn/zhangdeman/api-doc/enums"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -39,32 +39,7 @@ func NewServerOption(port int, optionList ...SetServerOptionFunc) *serverOption
|
||||
|
||||
func newServerOption(port int, optionList ...SetServerOptionFunc) *serverOption {
|
||||
option := &serverOption{
|
||||
swaggerUiTheme: apiDocEnum.SwaggerUIThemeRedocFree.String(),
|
||||
swaggerBaseUri: "/doc/swagger",
|
||||
globalMiddlewareList: nil,
|
||||
disableSwaggerDoc: false,
|
||||
serverInfo: &apiDocDefine.Info{
|
||||
Description: "这是一个微服务,提供一些必要的的数据接口功能",
|
||||
Title: "微服务接口文档",
|
||||
TermsOfService: "",
|
||||
Contact: &apiDocDefine.Contact{
|
||||
Name: "开发人员",
|
||||
Url: "",
|
||||
Email: "developer@example.com",
|
||||
},
|
||||
License: &apiDocDefine.License{
|
||||
Name: consts.LicenseApache20,
|
||||
Url: consts.LicenseUrlTable[consts.LicenseApache20],
|
||||
},
|
||||
Version: "0.0.1",
|
||||
},
|
||||
serverList: []*apiDocDefine.ServerItem{
|
||||
{
|
||||
Url: fmt.Sprintf("http://127.0.0.1:%d", port),
|
||||
Description: "测试服务器",
|
||||
Variables: nil,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, opt := range optionList {
|
||||
if nil == opt {
|
||||
@@ -83,9 +58,28 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
option := newServerOption(port, optionList...)
|
||||
|
||||
// 启用文档, 初始化文档
|
||||
if option.docConfig.Enable {
|
||||
if len(option.docConfig.BaseUri) == 0 {
|
||||
option.docConfig.BaseUri = "/_doc/swagger" // 未指定文档基础路径, 则以默认值作为基础路径
|
||||
}
|
||||
if len(option.docConfig.Flag) == 0 {
|
||||
option.docConfig.Flag = fmt.Sprintf("%v", port) // 未指定文档实例标识, 则以监听端口作为实例
|
||||
}
|
||||
// base_uri 拼接 文档标识, 作为最终 baseUri
|
||||
option.docConfig.BaseUri = fmt.Sprintf("%v/%v", strings.TrimRight(option.docConfig.BaseUri, "/"), strings.Trim(option.docConfig.Flag, "/"))
|
||||
optionFuncList := []openapi.OptionFunc{
|
||||
openapi.WithInfo(option.docConfig.Info),
|
||||
openapi.WithSecurity(option.docConfig.SecuritySchemes),
|
||||
openapi.WithCommonParameter(option.docConfig.CommonParameter),
|
||||
}
|
||||
if nil != option.docConfig.ServerList {
|
||||
optionFuncList = append(optionFuncList, openapi.WithServers(*option.docConfig.ServerList))
|
||||
}
|
||||
openapi.DocManager.NewOpenApiDoc(option.docConfig.Flag, optionFuncList...)
|
||||
}
|
||||
s := &server{
|
||||
router: gin.Default(),
|
||||
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, apiDocEnum.SwaggerUITheme(option.swaggerUiTheme)),
|
||||
port: port,
|
||||
option: option,
|
||||
lock: &sync.RWMutex{},
|
||||
@@ -108,7 +102,6 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
type server struct {
|
||||
router *gin.Engine
|
||||
port int
|
||||
uiInstance *apiDoc.SwaggerUI
|
||||
option *serverOption
|
||||
commonParam map[string]GetCommonParam // 结构体字段名, 注意, 不是TAG
|
||||
lock *sync.RWMutex
|
||||
@@ -157,10 +150,48 @@ func (s *server) getGlobalMiddlewareList(option *serverOption) {
|
||||
s.globalMiddlewareDescList = s.getMiddlewareDescList(s.globalMiddlewareList)
|
||||
}
|
||||
|
||||
// RegisterDocHandler 注册文档路由
|
||||
func (s *server) RegisterDocHandler() {
|
||||
if !s.option.docConfig.Enable {
|
||||
return
|
||||
}
|
||||
|
||||
s.router.GET(s.option.docConfig.BaseUri+"/*any", func(ctx *gin.Context) {
|
||||
if ctx.Request.RequestURI == s.option.docConfig.BaseUri+"/doc.json" || ctx.Request.RequestURI == s.option.docConfig.BaseUri+"/knife4go/doc.json" {
|
||||
// 默认swagger, 通过此接口读取文档数据
|
||||
ctx.JSON(http.StatusOK, openapi.DocManager.DocData(s.option.docConfig.Flag))
|
||||
ctx.Abort()
|
||||
}
|
||||
if ctx.Request.RequestURI == "/doc/swagger/openapi.json" {
|
||||
// knife4go 文档通过此接口读取文档列表
|
||||
ctx.JSON(http.StatusOK, []map[string]any{
|
||||
{
|
||||
"name": "服务文档",
|
||||
"url": "doc.json",
|
||||
"swaggerVersion": enums.SwaggerDocVersion3.String(),
|
||||
},
|
||||
})
|
||||
ctx.Abort()
|
||||
}
|
||||
}, apiDoc.NewSwaggerUI(fmt.Sprintf("[%v]接口文档", s.option.docConfig.Flag), s.option.docConfig.BaseUri, enums.SwaggerUITheme(s.option.docConfig.UiTheme)).Handler())
|
||||
s.router.GET("/swagger-resources", func(ctx *gin.Context) { // lucky UI获取分组信息
|
||||
ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*") // 允许访问所有域
|
||||
ctx.JSON(http.StatusOK, []map[string]any{
|
||||
{
|
||||
"name": "服务文档",
|
||||
"url": s.option.docConfig.BaseUri + "/doc.json",
|
||||
"swaggerVersion": enums.SwaggerDocVersion3.String(),
|
||||
},
|
||||
})
|
||||
// ctx.JSON(http.StatusOK, swaggerInstance.docInstance.Data())
|
||||
})
|
||||
}
|
||||
|
||||
// Start 启动服务
|
||||
func (s *server) Start() {
|
||||
// 注册文档
|
||||
s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri)
|
||||
s.RegisterDocHandler()
|
||||
// s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri)
|
||||
gracefulServer := graceful.NewServer(fmt.Sprintf(":%d", s.port), s.Router())
|
||||
defer func() {
|
||||
_ = gracefulServer.Close()
|
||||
@@ -215,7 +246,7 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co
|
||||
// 设置 logic 函数描述
|
||||
apiMiddlewareList = append(apiMiddlewareList, runtime.FuncForPC(itemUriCfg.ApiLogicFunc.Func.Pointer()).Name())
|
||||
|
||||
_ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
|
||||
// _ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
|
||||
// 普通 HTTP 请求
|
||||
handleFunc := s.RequestHandler(itemUriCfg)
|
||||
if itemUriCfg.IsSse {
|
||||
@@ -228,6 +259,13 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co
|
||||
routerPrefix = "/" + strings.TrimSuffix(strings.TrimPrefix(routerPrefix, "/"), "/")
|
||||
}
|
||||
fullUriPath := routerPrefix + "/" + strings.TrimPrefix(itemUriCfg.Path, "/")
|
||||
// 注册接口文档
|
||||
_ = openapi.DocManager.AddApiDoc(s.option.docConfig.Flag, apiDocDefine.UriConfig{
|
||||
Path: fullUriPath,
|
||||
RequestMethod: method,
|
||||
TagList: itemUriCfg.TagList,
|
||||
Desc: itemUriCfg.Desc,
|
||||
}, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
|
||||
s.uriTable[fullUriPath] = itemUriCfg // 注册路由时存储 接口路径 => 接口配置的信息
|
||||
s.consoleOutput = append(s.consoleOutput, []any{
|
||||
fullUriPath,
|
||||
|
||||
Reference in New Issue
Block a user