github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/courier/swagger/swagger.go (about) 1 package swagger 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io/ioutil" 8 "strings" 9 10 "github.com/artisanhe/tools/courier" 11 "github.com/artisanhe/tools/courier/httpx" 12 "github.com/artisanhe/tools/courier/transport_http" 13 ) 14 15 func getSwaggerJSON() []byte { 16 data, err := ioutil.ReadFile("./swagger.json") 17 if err != nil { 18 return data 19 } 20 return data 21 } 22 23 var SwaggerRouter = courier.NewRouter() 24 25 func init() { 26 SwaggerRouter.Register(courier.NewRouter(Swagger{})) 27 SwaggerRouter.Register(courier.NewRouter(SwaggerDoc{})) 28 SwaggerRouter.Register(courier.NewRouter(SwaggerUIBundle{})) 29 } 30 31 type Swagger struct { 32 httpx.MethodGet 33 } 34 35 func (s Swagger) Output(c context.Context) (interface{}, error) { 36 json := &JSONBytes{} 37 json.Write(getSwaggerJSON()) 38 return json, nil 39 } 40 41 // swagger:strfmt json 42 type JSONBytes struct { 43 bytes.Buffer 44 } 45 46 func (JSONBytes) ContentType() string { 47 return httpx.MIMEJSON 48 } 49 50 type SwaggerDoc struct { 51 httpx.MethodGet 52 } 53 54 func (SwaggerDoc) Path() string { 55 return "/doc" 56 } 57 58 func (s SwaggerDoc) Output(c context.Context) (interface{}, error) { 59 html := &httpx.HTML{} 60 u := transport_http.GetRequest(c).URL 61 62 html.WriteString(`<!-- HTML for static distribution bundle build --> 63 <!DOCTYPE html> 64 <html lang="en"> 65 <head> 66 <meta charset="UTF-8"> 67 <title>Swagger UI</title> 68 <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.17.4/swagger-ui.css" > 69 <style> 70 html 71 { 72 box-sizing: border-box; 73 overflow: -moz-scrollbars-vertical; 74 overflow-y: scroll; 75 } 76 77 *, 78 *:before, 79 *:after 80 { 81 box-sizing: inherit; 82 } 83 84 body 85 { 86 margin:0; 87 background: #fafafa; 88 } 89 </style> 90 </head> 91 92 <body> 93 <div id="swagger-ui"></div> 94 <script src="` + strings.TrimSuffix(u.Path, s.Path()) + "/swagger-ui-bundle.js" + `"> </script> 95 <script src="//cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.17.4/swagger-ui-standalone-preset.js"> </script> 96 <script> 97 window.onload = function() { 98 // Build a system 99 var ui = SwaggerUIBundle({ 100 url: "` + strings.TrimSuffix(u.Path, s.Path()) + `", 101 dom_id: '#swagger-ui', 102 deepLinking: true, 103 presets: [ 104 SwaggerUIBundle.presets.apis, 105 SwaggerUIStandalonePreset 106 ], 107 plugins: [ 108 SwaggerUIBundle.plugins.DownloadUrl 109 ], 110 layout: "StandaloneLayout" 111 }) 112 113 window.ui = ui 114 } 115 </script> 116 </body> 117 </html>`) 118 119 return html, nil 120 } 121 122 type SwaggerUIBundle struct { 123 httpx.MethodGet 124 } 125 126 func (SwaggerUIBundle) Path() string { 127 return "/swagger-ui-bundle.js" 128 } 129 130 func (s SwaggerUIBundle) Output(c context.Context) (interface{}, error) { 131 html := &httpx.HTML{} 132 133 b, err := Asset("swagger-ui-bundle.js") 134 if err != nil { 135 return nil, fmt.Errorf("asset: %v", err) 136 } 137 138 _, err = html.Write(b) 139 if err != nil { 140 return nil, fmt.Errorf("html write: %v", err) 141 } 142 143 return html, nil 144 }