6

I'm using go-swagger to generate the API server. I noticed that the json generated from swagger.yml is being kept in restapi/embedded_spec.go.

What's the best way to expose that JSON spec so my ReactJS client can access it?

So far I've had to use swagger serve swagger.yml --port=50000 and point javacript client to localhost:50000/swagger.json. I'm looking for a way to serve that JSON straight from restapi/embedded_spec.go via my API.

Maklaus
  • 538
  • 2
  • 16
  • 37
  • As far I can see there is no out of the box option to make this happen. But go swagger is super flexible, you could, for instance, create a template with the swagger.json content. From the docs: When generating a server or client you can specify a directory to load custom templates from with --template-dir. It will recursively read all the .gotmpl files in the directory and load them as templates. Another option is to customize the server generation step using flags. https://goswagger.io/faq/faq_server.html – Oscar Nevarez Sep 10 '18 at 16:35

1 Answers1

1

Maybe this is from an old code point of view, but currently when running a server the swagger.json file is provided as well.

$ go run cmd/swagger-petstore-server/main.go --port=50000
2018/09/20 12:48:35 Serving swagger petstore at http://127.0.0.1:50000

$ curl http://127.0.0.1:50000/swagger.json
{
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
...
Kevin Sandow
  • 4,003
  • 1
  • 20
  • 33