加载API定义失败,出现了go-openapi/runtime/middleware错误。

huangapple go评论86阅读模式
英文:

Failed to load API definition with go-openapi/runtime/middleware

问题

我正在使用golang的http/netgithub.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时,我得到以下结果:

加载API定义失败,出现了go-openapi/runtime/middleware错误。

乍一看,我觉得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:

加载API定义失败,出现了go-openapi/runtime/middleware错误。

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

huangapple
  • 本文由 发表于 2022年11月30日 19:13:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/74626856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定