github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_server_swagger.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package ghttp 8 9 import ( 10 "github.com/wangyougui/gf/v2/text/gstr" 11 ) 12 13 const ( 14 swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}` 15 swaggerUITemplate = ` 16 <!DOCTYPE html> 17 <html> 18 <head> 19 <title>API Reference</title> 20 <meta charset="utf-8"/> 21 <meta name="viewport" content="width=device-width, initial-scale=1"> 22 <style> 23 body { 24 margin: 0; 25 padding: 0; 26 } 27 </style> 28 </head> 29 <body> 30 <redoc spec-url="{SwaggerUIDocUrl}" show-object-schema-examples="true"></redoc> 31 <script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"> </script> 32 </body> 33 </html> 34 ` 35 ) 36 37 // swaggerUI is a build-in hook handler for replace default swagger json URL to local openapi json file path. 38 // This handler makes sense only if the openapi specification automatic producing configuration is enabled. 39 func (s *Server) swaggerUI(r *Request) { 40 if s.config.OpenApiPath == "" { 41 return 42 } 43 var templateContent = swaggerUITemplate 44 if s.config.SwaggerUITemplate != "" { 45 templateContent = s.config.SwaggerUITemplate 46 } 47 48 if r.StaticFile != nil && r.StaticFile.File != nil && r.StaticFile.IsDir { 49 content := gstr.ReplaceByMap(templateContent, map[string]string{ 50 swaggerUIDocURLPlaceHolder: s.config.OpenApiPath, 51 }) 52 r.Response.Write(content) 53 r.ExitAll() 54 } 55 }