From ef673eb9905426fce1e38776ff8a2b78d1f11a02 Mon Sep 17 00:00:00 2001 From: houseme Date: Thu, 21 Dec 2023 23:18:02 +0800 Subject: [PATCH] feat: improve code for swagger and add swagger template --- example/httpserver/swagger/main.go | 4 ++++ example/httpserver/swagger_set_js/main.go | 29 ++++++++++++++++++++++- net/ghttp/ghttp_server_config.go | 6 ++--- net/ghttp/ghttp_server_config_mess.go | 6 ++--- net/ghttp/ghttp_server_swagger.go | 12 ++++------ 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/example/httpserver/swagger/main.go b/example/httpserver/swagger/main.go index 2105fcba7be..b494c73d2e3 100644 --- a/example/httpserver/swagger/main.go +++ b/example/httpserver/swagger/main.go @@ -8,17 +8,21 @@ import ( "github.com/gogf/gf/v2/net/ghttp" ) +// HelloReq hello request type HelloReq struct { g.Meta `path:"/hello" method:"get" sort:"1"` Name string `v:"required" dc:"Your name"` } +// HelloRes hello response type HelloRes struct { Reply string `dc:"Reply content"` } +// Hello Controller type Hello struct{} +// Say function func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) { g.Log().Debugf(ctx, `receive say: %+v`, req) res = &HelloRes{ diff --git a/example/httpserver/swagger_set_js/main.go b/example/httpserver/swagger_set_js/main.go index fc98c025166..0e9741fac1f 100644 --- a/example/httpserver/swagger_set_js/main.go +++ b/example/httpserver/swagger_set_js/main.go @@ -8,17 +8,21 @@ import ( "github.com/gogf/gf/v2/net/ghttp" ) +// HelloReq hello request type HelloReq struct { g.Meta `path:"/hello" method:"get" sort:"1"` Name string `v:"required" dc:"Your name"` } +// HelloRes hello response type HelloRes struct { Reply string `dc:"Reply content"` } +// Hello Controller type Hello struct{} +// Say function func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) { g.Log().Debugf(ctx, `receive say: %+v`, req) res = &HelloRes{ @@ -36,7 +40,30 @@ func main() { ) }) - s.SetSwaggerJsURL("https://unpkg.com/redoc@2.0.0/bundles/redoc.standalone.js") + s.SetSwaggerTemplate(` + + + + + + + SwaggerUI + + + +
+ + + + +`) // or with static files // s.AddStaticPath("/js", "js") // s.SetSwaggerJsURL("/js/redoc.standalone.js") diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 9f0e8796035..f6f20ad210b 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -222,9 +222,9 @@ type ServerConfig struct { // API & Swagger. // ====================================================================================================== - OpenApiPath string `json:"openapiPath"` // OpenApiPath specifies the OpenApi specification file path. - SwaggerPath string `json:"swaggerPath"` // SwaggerPath specifies the swagger UI path for route registering. - SwaggerJsURL string `json:"swaggerJsURL"` // SwaggerJsURL specifies the swagger UI js URL for document. https://www.npmjs.com/package/redoc + OpenApiPath string `json:"openapiPath"` // OpenApiPath specifies the OpenApi specification file path. + SwaggerPath string `json:"swaggerPath"` // SwaggerPath specifies the swagger UI path for route registering. + SwaggerTemplate string `json:"swaggerTemplate"` // SwaggerTemplate specifies the swagger UI custom template // ====================================================================================================== // Other. diff --git a/net/ghttp/ghttp_server_config_mess.go b/net/ghttp/ghttp_server_config_mess.go index 326ec780b68..58c79b9adff 100644 --- a/net/ghttp/ghttp_server_config_mess.go +++ b/net/ghttp/ghttp_server_config_mess.go @@ -32,9 +32,9 @@ func (s *Server) SetSwaggerPath(path string) { s.config.SwaggerPath = path } -// SetSwaggerJsURL sets the SwaggerJsURL for server. -func (s *Server) SetSwaggerJsURL(jsURL string) { - s.config.SwaggerJsURL = jsURL +// SetSwaggerTemplate sets the Swagger template for server. +func (s *Server) SetSwaggerTemplate(swaggerTemplate string) { + s.config.SwaggerTemplate = swaggerTemplate } // SetOpenApiPath sets the OpenApiPath for server. diff --git a/net/ghttp/ghttp_server_swagger.go b/net/ghttp/ghttp_server_swagger.go index bec91c7bf84..79f9d54e160 100644 --- a/net/ghttp/ghttp_server_swagger.go +++ b/net/ghttp/ghttp_server_swagger.go @@ -12,7 +12,6 @@ import ( const ( swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}` - swaggerUIDocJsPlaceHolder = `{SwaggerUIDocJs}` swaggerUITemplate = ` @@ -29,7 +28,7 @@ const ( - + ` @@ -41,15 +40,14 @@ func (s *Server) swaggerUI(r *Request) { if s.config.OpenApiPath == "" { return } - - if s.config.SwaggerJsURL == "" { - s.config.SwaggerJsURL = "https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js" + var templateContent = swaggerUITemplate + if s.config.SwaggerTemplate != "" { + templateContent = s.config.SwaggerTemplate } if r.StaticFile != nil && r.StaticFile.File != nil && r.StaticFile.IsDir { - content := gstr.ReplaceByMap(swaggerUITemplate, map[string]string{ + content := gstr.ReplaceByMap(templateContent, map[string]string{ swaggerUIDocURLPlaceHolder: s.config.OpenApiPath, - swaggerUIDocJsPlaceHolder: s.config.SwaggerJsURL, }) r.Response.Write(content) r.ExitAll()