Skip to content

Commit

Permalink
feat: improve code for swagger and add swagger template
Browse files Browse the repository at this point in the history
  • Loading branch information
houseme committed Dec 21, 2023
1 parent 326317b commit ef673eb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
4 changes: 4 additions & 0 deletions example/httpserver/swagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
29 changes: 28 additions & 1 deletion example/httpserver/swagger_set_js/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -36,7 +40,30 @@ func main() {
)
})

s.SetSwaggerJsURL("https://unpkg.com/redoc@2.0.0/bundles/redoc.standalone.js")
s.SetSwaggerTemplate(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI"/>
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui.min.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '{SwaggerUIDocUrl}',
dom_id: '#swagger-ui',
});
};
</script>
</body>
</html>
`)
// or with static files
// s.AddStaticPath("/js", "js")
// s.SetSwaggerJsURL("/js/redoc.standalone.js")
Expand Down
6 changes: 3 additions & 3 deletions net/ghttp/ghttp_server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions net/ghttp/ghttp_server_config_mess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 5 additions & 7 deletions net/ghttp/ghttp_server_swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

const (
swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}`
swaggerUIDocJsPlaceHolder = `{SwaggerUIDocJs}`
swaggerUITemplate = `
<!DOCTYPE html>
<html>
Expand All @@ -29,7 +28,7 @@ const (
</head>
<body>
<redoc spec-url="{SwaggerUIDocUrl}" show-object-schema-examples="true"></redoc>
<script src="{SwaggerUIDocJs}"> </script>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"> </script>
</body>
</html>
`
Expand All @@ -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()
Expand Down

0 comments on commit ef673eb

Please sign in to comment.