Swagger UI updates

This commit is contained in:
John Guo
2022-03-04 11:36:05 +08:00
parent 84c0f456c0
commit d5d199ebef

View File

@ -11,8 +11,10 @@ import (
)
const (
swaggerUIDefaultURL = `https://petstore.swagger.io/v2/swagger.json`
swaggerUITemplate = `
swaggerUIDocName = `redoc.standalone.js`
swaggerUIDocNamePlaceHolder = `{SwaggerUIDocName}`
swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}`
swaggerUITemplate = `
<!DOCTYPE html>
<html>
<head>
@ -27,8 +29,8 @@ const (
</style>
</head>
<body>
<redoc spec-url="https://petstore.swagger.io/v2/swagger.json"></redoc>
<script src="redoc.standalone.js"> </script>
<redoc spec-url="{SwaggerUIDocUrl}"></redoc>
<script src="{SwaggerUIDocName}"> </script>
</body>
</html>
`
@ -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()
}
}