英文:
Failed to load API definition with go-openapi/runtime/middleware
问题
我正在使用golang的http/net
和github.com/go-openapi/runtime/middleware
来探索在go中使用Swagger。使用以下代码:
func setupAndRunHTTPServer() {
httpPort := envString("HTTP_PORT", defaultHTTPPort)
httpAddr := net.JoinHostPort("localhost", httpPort)
service := planting.NewPlantingService()
eps := endpoints.NewEndpointSet(*service)
httpServerMux := transport.NewHTTPHandler(eps)
// 设置Swagger文档和UI。
var sh http.Handler = middleware.SwaggerUI(middleware.SwaggerUIOpts{}, nil)
httpServerMux.Handle("/docs", sh)
// HTTP监听器挂载我们创建的Go kit HTTP处理程序。
httpListener, err := net.Listen("tcp", httpAddr)
if err != nil {
os.Exit(1)
}
// 使用transport包中创建的net/http.ServerMux进行服务。
err = http.Serve(httpListener, httpServerMux)
if err != nil {
os.Exit(2)
}
defer httpListener.Close()
}
当我访问http://localhost:8088/docs
时,我得到以下结果:
乍一看,我觉得net/http
库和middleware
无法找到相对于服务器根目录的swagger.json
文件。我将该文件放在以下位置:
matthew.hoggan in ~/go/src/project/root on TemplateServer*
⚡ ls -l ./docs/swagger.json
-rw-r--r-- 1 matthew.hoggan staff 3880 Nov 30 04:47 ./docs/swagger.json
我的问题是,在部署之前,我如何告诉middleware
在当前目录中找到swagger.json
文件的位置,以便在测试克隆的项目时使用?
英文:
I am using golang's http/net
along with github.com/go-openapi/runtime/middleware
to explore Swagger within go
. Using the following code:
func setupAndRunHTTPServer() {
httpPort := envString("HTTP_PORT", defaultHTTPPort)
httpAddr := net.JoinHostPort("localhost", httpPort)
service := planting.NewPlantingService()
eps := endpoints.NewEndpointSet(*service)
httpServerMux := transport.NewHTTPHandler(eps)
// Setup the swagger docs and UI.
var sh http.Handler = middleware.SwaggerUI(middleware.SwaggerUIOpts{}, nil)
httpServerMux.Handle("/docs", sh)
// The HTTP listener mounts the Go kit HTTP handler we created.
httpListener, err := net.Listen("tcp", httpAddr)
if err != nil {
os.Exit(1)
}
// Serve using the net/http.ServerMux creaded in transport package.
err = http.Serve(httpListener, httpServerMux)
if err != nil {
os.Exit(2)
}
defer httpListener.Close()
}
When I go to http://localhost:8088/docs
I get the following:
At first glance it appears to me that the net/http
library along with the middleware
cannot find the swagger.json
file relative to the server root. I have that file located in:
matthew.hoggan in ~/go/src/project/root on TemplateServer*
⚡ ls -l ./docs/swagger.json
-rw-r--r-- 1 matthew.hoggan staff 3880 Nov 30 04:47 ./docs/swagger.json
My question is, how do I tell the middleware
, where to find the swagger.json
situated in the current directory while testing the cloned project prior to deploying it?
答案1
得分: 1
根据源代码,你需要执行以下操作:
var sh http.Handler = middleware.SwaggerUI(middleware.SwaggerUIOpts{SpecURL: "./docs/swagger.json"}, nil)
如果未指定SpecURL,它将默认为/swagger.json
。
英文:
from the source code it appears you need to do the following:
var sh http.Handler = middleware.SwaggerUI(middleware.SwaggerUIOpts{SpecURL: "./docs/swagger.json"}, nil)
If SpecUrl is not specified it will default to:
/swagger.json
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论