0

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": ""
                }
            }
        }
    }
}`,}
Sakib
  • 1,503
  • 4
  • 26
  • 39

2 Answers2

0

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")
davidcv5
  • 1,027
  • 8
  • 13
0

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

Casual Jim
  • 1,179
  • 7
  • 13
  • I actually started looking into that too after this. It seems to be better but getting swagger for go setup is just difficult and obscure. I tried to generate the spec but it basically prints `{"swagger":"2.0","paths":{},"definitions":{}}`. I am doing it in the same directory as the main.go and have the annotation for `go:generate` on top of my main.go file too. Do you know any good tutorials or wikis that go over this in a more detailed way? – Sakib Oct 17 '16 at 05:41