我有一个用 Go 编写的服务,它也使用 go 模板作为前端。该服务被我们的外部第三方用作查找资料和搜索的门户。还有另一个服务是用于处理订单的休息 API。门户服务有一个页面,您可以在其中查找 API 文档。我只有一个 API 版本,我使用 SwaggerUI 来显示 API 文档。我必须创建一个新端点并使其成为新 API 版本的一部分。现在我想展示一个新的 API 版本,以及支持旧客户端的旧版本。像这样的东西:
所以当用户点击门户网站上的按钮查看文档时,请求由门户中的这个函数处理(注意:我已经重构了这个函数以支持多个 url):
func getDocs(c echo.Context) error {
source := c.Get(auth.SourceName).(string)
key := c.Get(auth.KeyName).(string)
jsonURLs := []DocsURL{
{
url: fmt.Sprintf("%s/0.1/docs?source=%s", config.baseURL, key),
name: "0.1"
},
{
url: fmt.Sprintf("%s/1.0/docs?source=%s", config.baseURL, key),
name: "0.1"
},
}
return c.Render(http.StatusOK, "docs/index", map[string]interface{}{
"source": source,
"key": key,
"pageType": "docs",
"jsonURLs": jsonURLs,
})
}
这是我使用 SwaggerUI 的模板中的脚本:
window.onload = function() {
const ui = SwaggerUIBundle({
urls: {{ .jsonURLs }},
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
supportedSubmitMethods: []
})
window.ui = ui
$('.tryitout').prop('disabled', true);
}
我需要根据环境(生产、登台、本地)生成链接。在后端生成数据并将其提供给模板进行显示也是有意义的。但这不起作用!
但是,如果我对链接中的链接进行硬编码,SwaggerUIBundle它可以工作,并且门户网站会显示正确的下拉菜单,并允许在版本之间切换并加载相应版本的文档。
func getDocs(c echo.Context) error {
source := c.Get(auth.SourceName).(string)
key := c.Get(auth.KeyName).(string)
return c.Render(http.StatusOK, "docs/index", map[string]interface{}{
"source": source,
"key": key,
"pageType": "docs",
})
}
这是为什么?有没有办法让第一个版本的代码工作?
我需要链接是动态的,最好在处理程序中生成。谢谢!
开心每一天1111
相关分类