// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghttp
import (
"fmt"
"github.com/gogf/gf/v2/text/gstr"
)
const (
swaggerUIDocName = `redoc.standalone.js`
swaggerUIDocNamePlaceHolder = `{SwaggerUIDocName}`
swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}`
swaggerUITemplate = `
API Reference
`
)
// swaggerUI is a build-in hook handler for replace default swagger json URL to local openapi json file path.
// This handler makes sense only if the openapi specification automatic producing configuration is enabled.
func (s *Server) swaggerUI(r *Request) {
if s.config.OpenApiPath == "" {
return
}
if r.StaticFile != nil && r.StaticFile.File != nil && r.StaticFile.IsDir {
content := gstr.ReplaceByMap(swaggerUITemplate, map[string]string{
swaggerUIDocURLPlaceHolder: s.config.OpenApiPath,
swaggerUIDocNamePlaceHolder: gstr.TrimRight(fmt.Sprintf(`//%s%s`, r.Host, r.Server.config.SwaggerPath), "/") + "/" + swaggerUIDocName,
})
r.Response.Write(content)
r.ExitAll()
}
}