基本介绍

GoFrameServer 默认自带的 OpenAPI 接口文档UI是 redoc 开源组件,该组件不支持页面 Try It Out 功能的。很多同学都在问,能否使用 SwaggerUI 页面来展示 OpenAPI 接口文档?有的企业内部并不支持连接外网的部分资源,那么能否将内部的接口文档UI替换为内部可访问的资源呢?

了解 OpenAPI 这个东东的小伙伴应该都知道, OpenAPI 只是通用的接口定义规范,而展示的接口文档UI是可以随便替换的, 并且这种UI界面以及平台还特别多!使用 GoFrame Server 来切换接口文档UI页面, 或者将接口文档对接到第三方接口文档平台 - 非常简单!详见示例: gf/example/httpserver/swagger-set-template/main.go

使用示例

咱们通过代码来展示一下,如何快速地将接口文档UI切换为 SwaggerUISwaggerUI 的相关链接:

main.go

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/net/ghttp"
  7. )
  8. // HelloReq hello request
  9. type HelloReq struct {
  10. g.Meta `path:"/hello" method:"get" sort:"1"`
  11. Name string `v:"required" dc:"Your name"`
  12. }
  13. // HelloRes hello response
  14. type HelloRes struct {
  15. Reply string `dc:"Reply content"`
  16. }
  17. // Hello Controller
  18. type Hello struct{}
  19. // Say function
  20. func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) {
  21. g.Log().Debugf(ctx, `receive say: %+v`, req)
  22. res = &HelloRes{
  23. Reply: fmt.Sprintf(`Hi %s`, req.Name),
  24. }
  25. return
  26. }
  27. const (
  28. MySwaggerUITemplate = `
  29. <!DOCTYPE html>
  30. <html lang="en">
  31. <head>
  32. <meta charset="utf-8" />
  33. <meta name="viewport" content="width=device-width, initial-scale=1" />
  34. <meta name="description" content="SwaggerUI"/>
  35. <title>SwaggerUI</title>
  36. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui.min.css" />
  37. </head>
  38. <body>
  39. <div id="swagger-ui"></div>
  40. <script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui-bundle.js" crossorigin></script>
  41. <script>
  42. window.onload = () => {
  43. window.ui = SwaggerUIBundle({
  44. url: '{SwaggerUIDocUrl}',
  45. dom_id: '#swagger-ui',
  46. });
  47. };
  48. </script>
  49. </body>
  50. </html>
  51. `
  52. )
  53. func main() {
  54. s := g.Server()
  55. s.Use(ghttp.MiddlewareHandlerResponse)
  56. s.Group("/", func(group *ghttp.RouterGroup) {
  57. group.Bind(
  58. new(Hello),
  59. )
  60. })
  61. s.SetSwaggerUITemplate(MySwaggerUITemplate)
  62. s.Run()
  63. }

config.yaml

  1. server:
  2. address: ":8199"
  3. openapiPath: "/api.json"
  4. swaggerPath: "/swagger"

我们这里只定义了一个 Hello 的接口。可以看到,我们通过一个接口来展示 SwaggerUIHTML 页面,并且将 OpenAPI 的接口文件路径定义为 /api.json,没有启用 Server 自带的UI页面。执行后,终端输出:

  1. 2022-05-18 20:41:09.160 [INFO] openapi specification is serving at address: http://127.0.0.1:8199/api.json
  2. 2022-05-18 20:41:09.161 [INFO] pid[57888]: http server started listening on [:8199]
  3. ADDRESS | METHOD | ROUTE | HANDLER | MIDDLEWARE
  4. ----------|--------|-----------|-----------------------------------------------------------------|--------------------
  5. :8199 | ALL | /* | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE
  6. ----------|--------|-----------|-----------------------------------------------------------------|--------------------
  7. :8199 | ALL | /api.json | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec |
  8. ----------|--------|-----------|-----------------------------------------------------------------|--------------------
  9. :8199 | GET | /hello | main.(*Hello).Say |
  10. ----------|--------|-----------|-----------------------------------------------------------------|--------------------
  11. :8199 | GET | /swagger | main.main.func1.1 |
  12. ----------|--------|-----------|-----------------------------------------------------------------|--------------------

戳此链接访问: http://127.0.0.1:8199/swagger/

页面展示如下:

接口文档-自定义UI - 图1

开发者自定义其他的接口文档UI类似如此。

常见UI模板

swagger-ui

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta name="description" content="SwaggerUI"/>
  7. <title>SwaggerUI</title>
  8. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui.min.css" />
  9. </head>
  10. <body>
  11. <div id="swagger-ui"></div>
  12. <script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui-bundle.js" crossorigin></script>
  13. <script>
  14. window.onload = () => {
  15. window.ui = SwaggerUIBundle({
  16. url: '{SwaggerUIDocUrl}',
  17. dom_id: '#swagger-ui',
  18. });
  19. };
  20. </script>
  21. </body>
  22. </html>

openapi-ui

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>openAPI UI</title>
  6. </head>
  7. <body>
  8. <div id="openapi-ui-container" spec-url="{SwaggerUIDocUrl}" theme="light"></div>
  9. <script src="https://cdn.jsdelivr.net/npm/openapi-ui-dist@latest/lib/openapi-ui.umd.js"></script>
  10. </body>
  11. </html>