393 Star 2.7K Fork 654

GVPJohn/gf

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ghttp_server_openapi.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghttp
import (
"context"
"github.com/gogf/gf/v2/net/goai"
"github.com/gogf/gf/v2/text/gstr"
)
// initOpenApi generates api specification using OpenApiV3 protocol.
func (s *Server) initOpenApi() {
if s.config.OpenApiPath == "" {
return
}
var (
ctx = context.TODO()
err error
methods []string
)
for _, item := range s.GetRoutes() {
switch item.Type {
case HandlerTypeMiddleware, HandlerTypeHook:
continue
}
if item.Handler.Info.IsStrictRoute {
methods = []string{item.Method}
if gstr.Equal(item.Method, defaultMethod) {
methods = SupportedMethods()
}
for _, method := range methods {
err = s.openapi.Add(goai.AddInput{
Path: item.Route,
Method: method,
Object: item.Handler.Info.Value.Interface(),
})
if err != nil {
s.Logger().Fatalf(ctx, `%+v`, err)
}
}
}
}
}
// openapiSpec is a build-in handler automatic producing for openapi specification json file.
func (s *Server) openapiSpec(r *Request) {
if s.config.OpenApiPath == "" {
r.Response.Write(`OpenApi specification file producing is disabled`)
} else {
r.Response.WriteJson(s.openapi)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/johng/gf.git
git@gitee.com:johng/gf.git
johng
gf
gf
master

搜索帮助