Skip to content

Commit

Permalink
Add group support
Browse files Browse the repository at this point in the history
  • Loading branch information
ccfish86 committed May 26, 2023
1 parent 00a3636 commit d12c251
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
35 changes: 35 additions & 0 deletions ui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
```go
// example
engine := gin.Default()
// app
appFizz := fizz.NewFromEngine(engine)
appInfos := &openapi.Info{
Title: "Fruits Market",
Description: `This is a sample Fruits market server.`,
Version: "1.0.0",
}
appFizz.GET("/app/openapi.json", nil, appFizz.OpenAPI(appInfos, "json"))

// h5
h5Fizz := fizz.NewFromEngine(engine)
h5Infos := &openapi.Info{
Title: "Fruits Market",
Description: `This is a sample Fruits market server.`,
Version: "1.0.0",
}
h5Fizz.GET("/h5/openapi.json", nil, h5Fizz.OpenAPI(h5Infos, "json"))

// add a OpenApi UI with groups
ui.AddUIGroupHandler(engine, "/doc", ui.SwaggerUrl{
Name: "app",
Url: "/app/openapi.json",
}, ui.SwaggerUrl{
Name: "H5",
Url: "/h5/openapi.json",
})

```

And then you can get a api document with ui like follows

![alt=api ui example](./images/example_1.jpg)
Binary file added ui/images/example_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 52 additions & 1 deletion ui/knife_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func AddUIHandler(ginEngine *gin.Engine, path string, openApiJsonPath string) {
}})
})

ginEngine.GET(path+"/index.html", func(c *gin.Context) {
// init swagger-ui index.html
docIndex, _ := url.JoinPath(path, "/index.html")
ginEngine.GET(docIndex, func(c *gin.Context) {
c.Writer.WriteHeader(200)
c.Writer.Write(docHtml)
c.Writer.Header().Add("Accept", "text/html")
Expand Down Expand Up @@ -64,5 +66,54 @@ func AddUIHandler(ginEngine *gin.Engine, path string, openApiJsonPath string) {

urlSubOauth, _ := url.JoinPath(path, "oauth")
ginEngine.StaticFS(urlSubOauth, http.FS(subOauth))
}

// AddUIGroupHandler adds handler that serves html for Swagger UI
func AddUIGroupHandler(ginEngine *gin.Engine, path string, groups ...SwaggerUrl) {

if len(groups) == 0 {
return
}

// for `v3/api-docs/swagger-config`, as springdoc
configPath, _ := url.JoinPath(path, "v3/api-docs/swagger-config")
ginEngine.GET(configPath, func(c *gin.Context) {
c.JSON(200, &SwaggerConfig{ConfigUrl: configPath, DisplayRequestDuration: true, OperationsSorter: "method", Urls: &groups})
})

// init swagger-ui index.html
docIndex, _ := url.JoinPath(path, "/index.html")
ginEngine.GET(docIndex, func(c *gin.Context) {
c.Writer.WriteHeader(200)
c.Writer.Write(docHtml)
c.Writer.Header().Add("Accept", "text/html")
c.Writer.Flush()
})

// webjars
subWebjars, err := fs.Sub(statics, "knife4go/webjars")
if err != nil {
panic(err)
}

urlSubWebJars, _ := url.JoinPath(path, "webjars")
ginEngine.StaticFS(urlSubWebJars, http.FS(subWebjars))

// img
subImg, err := fs.Sub(statics, "knife4go/img")
if err != nil {
panic(err)
}

urlSubImg, _ := url.JoinPath(path, "img")
ginEngine.StaticFS(urlSubImg, http.FS(subImg))

// oauth
subOauth, err := fs.Sub(statics, "knife4go/oauth")
if err != nil {
panic(err)
}

urlSubOauth, _ := url.JoinPath(path, "oauth")
ginEngine.StaticFS(urlSubOauth, http.FS(subOauth))
}

0 comments on commit d12c251

Please sign in to comment.