2021-10-02 22:34:39 +08:00
|
|
|
// 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 (
|
2021-10-06 15:03:00 +08:00
|
|
|
"context"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
|
|
|
"github.com/gogf/gf/v2/protocol/goai"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2021-10-02 22:34:39 +08:00
|
|
|
)
|
|
|
|
|
2021-10-03 00:22:06 +08:00
|
|
|
// initOpenApi generates api specification using OpenApiV3 protocol.
|
2021-10-02 22:34:39 +08:00
|
|
|
func (s *Server) initOpenApi() {
|
2021-10-03 00:22:06 +08:00
|
|
|
if s.config.OpenApiPath == "" {
|
|
|
|
return
|
|
|
|
}
|
2021-10-02 22:34:39 +08:00
|
|
|
var (
|
2021-10-06 15:03:00 +08:00
|
|
|
ctx = context.TODO()
|
2021-10-02 22:34:39 +08:00
|
|
|
err error
|
|
|
|
method string
|
|
|
|
)
|
|
|
|
for _, item := range s.GetRoutes() {
|
2021-10-06 14:22:58 +08:00
|
|
|
switch item.Type {
|
|
|
|
case HandlerTypeMiddleware, HandlerTypeHook:
|
|
|
|
continue
|
|
|
|
}
|
2021-10-02 22:34:39 +08:00
|
|
|
method = item.Method
|
|
|
|
if gstr.Equal(method, defaultMethod) {
|
2021-10-06 14:52:29 +08:00
|
|
|
method = ""
|
2021-10-02 22:34:39 +08:00
|
|
|
}
|
|
|
|
if item.Handler.Info.Func == nil {
|
|
|
|
err = s.openapi.Add(goai.AddInput{
|
|
|
|
Path: item.Route,
|
|
|
|
Method: method,
|
|
|
|
Object: item.Handler.Info.Value.Interface(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-10-06 15:03:00 +08:00
|
|
|
s.Logger().Fatalf(ctx, `%+v`, err)
|
2021-10-02 22:34:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 00:22:06 +08:00
|
|
|
// openapiSpec is a build-in handler automatic producing for openapi specification json file.
|
|
|
|
func (s *Server) openapiSpec(r *Request) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if s.config.OpenApiPath == "" {
|
|
|
|
r.Response.Write(`OpenApi specification file producing is disabled`)
|
|
|
|
} else {
|
|
|
|
err = r.Response.WriteJson(s.openapi)
|
|
|
|
}
|
|
|
|
|
2021-10-02 22:34:39 +08:00
|
|
|
if err != nil {
|
|
|
|
intlog.Error(r.Context(), err)
|
|
|
|
}
|
|
|
|
}
|