github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/openapi/swaggerui.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package openapi 15 16 import ( 17 "bytes" 18 "html/template" 19 ) 20 21 const ( 22 defaultSwaggerHost = "https://petstore3.swagger.io" 23 swaggerUITemplate = ` 24 <!-- HTML for static distribution bundle build --> 25 <!DOCTYPE html> 26 <html lang="en"> 27 <head> 28 <meta charset="UTF-8"> 29 <title>API documentation</title> 30 <link rel="stylesheet" type="text/css" href="{{ .SwaggerHost }}/swagger-ui.css" > 31 <link rel="icon" type="image/png" href="{{ .SwaggerHost }}/favicon-32x32.png" sizes="32x32" /> 32 <link rel="icon" type="image/png" href="{{ .SwaggerHost }}/favicon-16x16.png" sizes="16x16" /> 33 <style> 34 html 35 { 36 box-sizing: border-box; 37 overflow: -moz-scrollbars-vertical; 38 overflow-y: scroll; 39 } 40 *, 41 *:before, 42 *:after 43 { 44 box-sizing: inherit; 45 } 46 body 47 { 48 margin:0; 49 background: #fafafa; 50 } 51 </style> 52 </head> 53 <body> 54 <div id="swagger-ui"></div> 55 <script src="{{ .SwaggerHost }}/swagger-ui-bundle.js"> </script> 56 <script src="{{ .SwaggerHost }}/swagger-ui-standalone-preset.js"> </script> 57 <script> 58 window.onload = function() { 59 // Begin Swagger UI call region 60 const ui = SwaggerUIBundle({ 61 "dom_id": "#swagger-ui", 62 deepLinking: true, 63 presets: [ 64 SwaggerUIBundle.presets.apis, 65 SwaggerUIStandalonePreset 66 ], 67 plugins: [ 68 SwaggerUIBundle.plugins.DownloadUrl 69 ], 70 layout: "StandaloneLayout", 71 url: "{{ .SpecJSONPath }}", 72 }) 73 // End Swagger UI call region 74 window.ui = ui 75 } 76 </script> 77 </body> 78 </html>` 79 ) 80 81 // SwaggerConfig configures the SwaggerDoc middlewares. 82 type SwaggerConfig struct { 83 // SpecJsonPath the url to find the spec 84 SpecJSONPath string 85 // SwaggerHost for the js that generates the swagger ui site, defaults to: http://petstore3.swagger.io/ 86 SwaggerHost string 87 } 88 89 // NewSwaggerConfig return swaggerConfig. 90 func NewSwaggerConfig(specJSONPath, swaggerHost string) *SwaggerConfig { 91 if swaggerHost == "" { 92 swaggerHost = defaultSwaggerHost 93 } 94 return &SwaggerConfig{ 95 SpecJSONPath: specJSONPath, 96 SwaggerHost: swaggerHost, 97 } 98 } 99 100 // GetSwaggerHTML returns the swagger ui html. 101 func GetSwaggerHTML(config *SwaggerConfig) (html string, err error) { 102 tmpl, err := template.New("swaggerdoc").Parse(swaggerUITemplate) 103 if err != nil { 104 return 105 } 106 107 buf := bytes.NewBuffer(nil) 108 err = tmpl.Execute(buf, config) 109 if err != nil { 110 return 111 } 112 return buf.String(), nil 113 }