Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose swagger.json with go-swagger server?

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.

like image 929
Maklaus Avatar asked Aug 29 '18 16:08

Maklaus


People also ask

How does Golang integrate with Swagger?

Getting go-swagger First step is getting the swagger command line interface. If you already have go , use go get : go get -u -v github.com/go-swagger/go-swagger/cmd/swagger swagger --help Usage: swagger [OPTIONS] <command> Swagger tries to support you as best as possible when building APIs.

How do I access Swagger on localhost?

Go to http://localhost:8000/ in your address bar. This address lets you view the local web server. By default, web servers default to the index. html file in the directory, so it will show the Swagger UI file automatically.

How do I access Swagger API?

From a browser, enter the URL to access the Swagger web page in the format https://<SnapCenter_IP_address_or_name>:8146/swagger/.


1 Answers

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"
  ],
...
like image 131
Kevin Sandow Avatar answered Nov 10 '22 21:11

Kevin Sandow