From d5d199ebefd16f8a8190e7fe9203159faa232cf4 Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 4 Mar 2022 11:36:05 +0800 Subject: [PATCH] Swagger UI updates --- net/ghttp/ghttp_server_swagger.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/net/ghttp/ghttp_server_swagger.go b/net/ghttp/ghttp_server_swagger.go index 7786a072b..846993c9a 100644 --- a/net/ghttp/ghttp_server_swagger.go +++ b/net/ghttp/ghttp_server_swagger.go @@ -11,8 +11,10 @@ import ( ) const ( - swaggerUIDefaultURL = `https://petstore.swagger.io/v2/swagger.json` - swaggerUITemplate = ` + swaggerUIDocName = `redoc.standalone.js` + swaggerUIDocNamePlaceHolder = `{SwaggerUIDocName}` + swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}` + swaggerUITemplate = ` @@ -27,8 +29,8 @@ const ( - - + + ` @@ -41,11 +43,11 @@ func (s *Server) swaggerUI(r *Request) { return } if r.StaticFile != nil && r.StaticFile.File != nil && r.StaticFile.IsDir { - r.Response.Write(gstr.Replace( - swaggerUITemplate, - swaggerUIDefaultURL, - s.config.OpenApiPath, - )) + content := gstr.ReplaceByMap(swaggerUITemplate, map[string]string{ + swaggerUIDocURLPlaceHolder: s.config.OpenApiPath, + swaggerUIDocNamePlaceHolder: gstr.TrimRight(r.GetUrl(), "/") + "/" + swaggerUIDocName, + }) + r.Response.Write(content) r.ExitAll() } }