I followed the instructions on this github page https://github.com/yvasiyarov/swagger using the example project and the api documentation generated fine. But when I run the last command to run and display the API page I always get 404 not found when I go there
The web.go file is essentially https://github.com/yvasiyarov/swagger/blob/master/web.go-example. Its basically the copy of the web.go-example in the repo... I dont understand why it would return 404.
Also do you have any suggestions for similar api documentation generators that are better? I dont want code generators like https://github.com/go-swagger/go-swagger. I want to generate api's based on my existing code
Here is the generated docs.go
package main
//This file is generated automatically. Do not try to edit it manually.
var resourceListingJson = `{
"apiVersion": "1.0.0",
"swaggerVersion": "1.2",
"basePath": "http://127.0.0.1:3000/",
"apis": [
{
"path": "/testapi",
"description": "Test API"
}
],
"info": {
"title": "Swagger Example API",
"description": "Swagger Example API",
"contact": "varyous@gmail.com",
"termsOfServiceUrl": "http://yvasiyarov.com/",
"license": "BSD",
"licenseUrl": "http://yvasiyarov.com/"
}
}`
var apiDescriptionsJson = map[string]string{"testapi":`{
"apiVersion": "1.0.0",
"swaggerVersion": "1.2",
"basePath": "http://127.0.0.1:3000/",
"resourcePath": "/testapi",
"produces": [
"application/json"
],
"apis": [
{
"path": "/testapi/get-string-by-int/{some_id}",
"description": "get string by ID",
"operations": [
{
"httpMethod": "GET",
"nickname": "GetStringByInt",
"type": "string",
"items": {},
"summary": "get string by ID",
"parameters": [
{
"paramType": "path",
"name": "some_id",
"description": "Some ID",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
}
],
"responseMessages": [
{
"code": 200,
"message": "",
"responseType": "object",
"responseModel": "string"
},
{
"code": 400,
"message": "We need ID!!",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
},
{
"code": 404,
"message": "Can not find ID",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
}
],
"produces": [
"application/json"
]
}
]
},
{
"path": "/testapi/get-struct-by-int/{some_id}",
"description": "get struct by ID",
"operations": [
{
"httpMethod": "GET",
"nickname": "GetStructByInt",
"type": "git.corp.adobe.com.personal.test1.StructureWithEmbededStructure",
"items": {},
"summary": "get struct by ID",
"parameters": [
{
"paramType": "path",
"name": "some_id",
"description": "Some ID",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
},
{
"paramType": "query",
"name": "offset",
"description": "Offset",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
},
{
"paramType": "query",
"name": "limit",
"description": "Offset",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
}
],
"responseMessages": [
{
"code": 200,
"message": "",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.StructureWithEmbededStructure"
},
{
"code": 400,
"message": "We need ID!!",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
},
{
"code": 404,
"message": "Can not find ID",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
}
],
"produces": [
"application/json"
]
}
]
},
{
"path": "/testapi/get-struct2-by-int/{some_id}",
"description": "get struct2 by ID",
"operations": [
{
"httpMethod": "GET",
"nickname": "GetStruct2ByInt",
"type": "git.corp.adobe.com.personal.test1.StructureWithEmbededPointer",
"items": {},
"summary": "get struct2 by ID",
"parameters": [
{
"paramType": "path",
"name": "some_id",
"description": "Some ID",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
},
{
"paramType": "query",
"name": "offset",
"description": "Offset",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
},
{
"paramType": "query",
"name": "limit",
"description": "Offset",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
}
],
"responseMessages": [
{
"code": 200,
"message": "",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.StructureWithEmbededPointer"
},
{
"code": 400,
"message": "We need ID!!",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
},
{
"code": 404,
"message": "Can not find ID",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
}
],
"produces": [
"application/json"
]
}
]
},
{
"path": "/testapi/get-simple-array-by-string/{some_id}",
"description": "get simple array by ID",
"operations": [
{
"httpMethod": "GET",
"nickname": "GetSimpleArrayByString",
"type": "array",
"items": {
"type": "string"
},
"summary": "get simple array by ID",
"parameters": [
{
"paramType": "path",
"name": "some_id",
"description": "Some ID",
"dataType": "string",
"type": "string",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
},
{
"paramType": "query",
"name": "offset",
"description": "Offset",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
},
{
"paramType": "query",
"name": "limit",
"description": "Offset",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
}
],
"responseMessages": [
{
"code": 200,
"message": "",
"responseType": "array",
"responseModel": "string"
},
{
"code": 400,
"message": "We need ID!!",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
},
{
"code": 404,
"message": "Can not find ID",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
}
],
"produces": [
"application/json"
]
}
]
},
{
"path": "/testapi/get-struct-array-by-string/{some_id}",
"description": "get struct array by ID",
"operations": [
{
"httpMethod": "GET",
"nickname": "GetStructArrayByString",
"type": "array",
"items": {
"$ref": "git.corp.adobe.com.personal.test1.SimpleStructureWithAnnotations"
},
"summary": "get struct array by ID",
"parameters": [
{
"paramType": "path",
"name": "some_id",
"description": "Some ID",
"dataType": "string",
"type": "string",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
},
{
"paramType": "query",
"name": "offset",
"description": "Offset",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
},
{
"paramType": "query",
"name": "limit",
"description": "Offset",
"dataType": "int",
"type": "int",
"format": "",
"allowMultiple": false,
"required": true,
"minimum": 0,
"maximum": 0
}
],
"responseMessages": [
{
"code": 200,
"message": "",
"responseType": "array",
"responseModel": "git.corp.adobe.com.personal.test1.SimpleStructureWithAnnotations"
},
{
"code": 400,
"message": "We need ID!!",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
},
{
"code": 404,
"message": "Can not find ID",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
}
],
"produces": [
"application/json"
]
}
]
},
{
"path": "/testapi/get-interface",
"description": "get interface",
"operations": [
{
"httpMethod": "GET",
"nickname": "GetInterface",
"type": "git.corp.adobe.com.personal.test1.InterfaceType",
"items": {},
"summary": "get interface",
"responseMessages": [
{
"code": 200,
"message": "",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.InterfaceType"
},
{
"code": 400,
"message": "We need ID!!",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
},
{
"code": 404,
"message": "Can not find ID",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
}
],
"produces": [
"application/json"
]
}
]
},
{
"path": "/testapi/get-simple-aliased",
"description": "get simple aliases",
"operations": [
{
"httpMethod": "GET",
"nickname": "GetSimpleAliased",
"type": "string",
"items": {},
"summary": "get simple aliases",
"responseMessages": [
{
"code": 200,
"message": "",
"responseType": "object",
"responseModel": "string"
},
{
"code": 400,
"message": "We need ID!!",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
},
{
"code": 404,
"message": "Can not find ID",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
}
],
"produces": [
"application/json"
]
}
]
},
{
"path": "/testapi/get-array-of-interfaces",
"description": "get array of interfaces",
"operations": [
{
"httpMethod": "GET",
"nickname": "GetArrayOfInterfaces",
"type": "array",
"items": {
"$ref": "git.corp.adobe.com.personal.test1.InterfaceType"
},
"summary": "get array of interfaces",
"responseMessages": [
{
"code": 200,
"message": "",
"responseType": "array",
"responseModel": "git.corp.adobe.com.personal.test1.InterfaceType"
},
{
"code": 400,
"message": "We need ID!!",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
},
{
"code": 404,
"message": "Can not find ID",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
}
],
"produces": [
"application/json"
]
}
]
},
{
"path": "/testapi/get-struct3",
"description": "get struct3",
"operations": [
{
"httpMethod": "GET",
"nickname": "GetStruct3",
"type": "git.corp.adobe.com.personal.test1.StructureWithSlice",
"items": {},
"summary": "get struct3",
"responseMessages": [
{
"code": 200,
"message": "",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.StructureWithSlice"
},
{
"code": 400,
"message": "We need ID!!",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
},
{
"code": 404,
"message": "Can not find ID",
"responseType": "object",
"responseModel": "git.corp.adobe.com.personal.test1.APIError"
}
],
"produces": [
"application/json"
]
}
]
}
],
"models": {
"git.corp.adobe.com.personal.test1.APIError": {
"id": "git.corp.adobe.com.personal.test1.APIError",
"properties": {
"ErrorCode": {
"type": "int",
"description": "",
"items": {},
"format": ""
},
"ErrorMessage": {
"type": "string",
"description": "",
"items": {},
"format": ""
}
}
},
"git.corp.adobe.com.personal.test1.InterfaceType": {
"id": "git.corp.adobe.com.personal.test1.InterfaceType",
"properties": null
},
"git.corp.adobe.com.personal.test1.SimpleStructureWithAnnotations": {
"id": "git.corp.adobe.com.personal.test1.SimpleStructureWithAnnotations",
"required": [
"Name"
],
"properties": {
"Name": {
"type": "string",
"description": "",
"items": {},
"format": ""
},
"id": {
"type": "int",
"description": "",
"items": {},
"format": ""
}
}
},
"git.corp.adobe.com.personal.test1.StructureWithEmbededPointer": {
"id": "git.corp.adobe.com.personal.test1.StructureWithEmbededPointer",
"properties": {
"Id": {
"type": "int",
"description": "",
"items": {},
"format": ""
},
"Name": {
"type": "array",
"description": "",
"items": {
"type": "byte"
},
"format": ""
}
}
},
"git.corp.adobe.com.personal.test1.StructureWithEmbededStructure": {
"id": "git.corp.adobe.com.personal.test1.StructureWithEmbededStructure",
"properties": {
"Id": {
"type": "int",
"description": "",
"items": {},
"format": ""
},
"Name": {
"type": "array",
"description": "",
"items": {
"type": "byte"
},
"format": ""
}
}
},
"git.corp.adobe.com.personal.test1.StructureWithSlice": {
"id": "git.corp.adobe.com.personal.test1.StructureWithSlice",
"properties": {
"Id": {
"type": "int",
"description": "",
"items": {},
"format": ""
},
"Name": {
"type": "array",
"description": "",
"items": {
"type": "byte"
},
"format": ""
}
}
}
}
}`,}
Go to the directory where you have docs.go
and web.go
, and assuming you downloaded the swagger
package using go get github.com/yvasiyarov/swagger
, the following should work for you:
$ go run web.go docs.go --staticPath=$GOPATH/src/github.com/yvasiyarov/swagger/swagger-ui --host=127.0.0.1 --port=3000
So basically, there are a couple of things happening here. If you look into web.go-example: https://github.com/yvasiyarov/swagger/blob/master/web.go-example#L14-L17, you can see it's using some defaults, which just won't work on your local environment, so you can do one of 2 things:
1 - Run the command showed above every time, so you specify the swagger-ui
path, and the host
and port
where you want to serve it.
2 - Modify web.go so it takes your default configuration and you can just run go run web.go docs.go
. Based on the command above, the changes will be like this:
var port = flag.String("port", "3000", "Port")
var staticContent = flag.String("staticPath", "$GOPATH/src/github.com/yvasiyarov/swagger/swagger-ui", "Path to folder with Swagger UI")
Then go to http://127.0.0.1:3000/swagger-ui/ and have fun!
Note: if you have your swagger
source somewhere else (not in your $GOPATH
, just make sure you use that route. i.e.
1 - In your command:
$ go run ... -staticPath=/some/path/.../swagger/swagger-ui
2 - Or in your web.go
var staticContent = flag.String("staticPath", "/some/path/.../swagger/swagger-ui", "Path to folder with Swagger UI")
For what its worth go-swagger also supports the inverse of code generation. You can find the documentation for it here: https://goswagger.io/generate/spec.html