Go-swagger: Question: How to use websockets with generated handlers?

Created on 20 Jan 2017  路  3Comments  路  Source: go-swagger/go-swagger

Generated handler functions have this form:

api.StaticSayHelloHandler = static.SayHelloHandlerFunc(func(params static.SayHelloParams) middleware.Responder {
  return middleware.NotImplemented("operation static.SayHello has not yet been implemented")
})

How you would implement websockets HTTP upgrade function with go-swagger? Is there any way to access ResponseWriter and Request? An upgrade looks like this:

func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
  conn, err := upgrader.Upgrade(w, r, nil)
  if err != nil {
    log.Println(err)
    return
  }
}
question

All 3 comments

you can implement a responder function which is: func(http.ResponseWriter, runtime.Producer)
https://github.com/go-openapi/runtime/blob/master/middleware/context.go#L59

The params struct has access to the http.Request and with the responder function you have access to the response writer

api.StaticSayHelloHandler = static.SayHelloHandlerFunc(func(params static.SayHelloParams) middleware.Responder {
  return middleware.ResponderFunc(func(rw http.ResponseWriter, _ runtime.Producer){
    conn, err := upgrader.Upgrade(rw, params.HTTPRequest, nil)
    if err != nil {
      log.Println(err)
      return
    }
  })
})

Works like a charm, thank you! ;)

@CuBiC3D, @casualjim, it looks like this is using gorilla websockets right? How would you recommend describing the websocket in yaml?

Was this page helpful?
0 / 5 - 0 ratings