Go API 在 html (Swagger UI) 中显示 swagger api 规范 (json) [英] Go API to show swagger api specification (json) in html (Swagger UI)

查看:71
本文介绍了Go API 在 html (Swagger UI) 中显示 swagger api 规范 (json)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务于特定端口的应用程序(大猩猩/多路复用器).我也有一个 json 文件形式的 swagger API 规范.是否有任何 go API 可以像 Spring Boot 一样从 JSON 文件生成 swagger UI 定义.我在看 https://github.com/go-swagger/go-swagger,但我无法在我的 go-lang 代码中使用它.看来这个 API 只能从命令行使用.

I have go application(gorilla/mux) that serves a specific port. I too have a swagger API specification in the form of json file. Is there any go API that can generate a swagger UI definition from the JSON file just like spring boot. I am looking at https://github.com/go-swagger/go-swagger, but i am not able to use this in my go-lang code. It seems this API can be used only from the command line.

推荐答案

如果您已经以 YAML 或 JSON 格式定义了 API,您可以使用静态dist"树从浏览器呈现它:

If you already have your APIs defined in YAML or JSON format, you can use the static 'dist' tree to render it from a browser:

https://github.com/swagger-api/swagger-ui/tree/master/dist

更新 index.html 以通过此标签指向您的 API 文档:

Update index.html to point to your API doc via this tag:

url: "https://petstore.swagger.io/v2/swagger.json",

如果您想从 go REST-API 服务器提供此静态树,请添加以下处理程序:

And if you want to serve this static tree from your go REST-API server, add the following handler:

fs := http.FileServer(http.Dir("dist"))
http.Handle("/swagger/", http.StripPrefix("/swagger/", fs))

这将在 /swagger/ 路由之外提供 swaggers 文档 - 从 dist 目录读取内容.

This will serve the swaggers docs off the /swagger/ route - reading the content from the dist directory.

这篇关于Go API 在 html (Swagger UI) 中显示 swagger api 规范 (json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆