Golang swagger返回404找不到

时间:2016-10-17 02:52:26

标签: go go-swagger

我使用示例项目按照此github页面https://github.com/yvasiyarov/swagger上的说明进行操作,并生成了api文档。但是,当我运行最后一个命令来运行并显示API页面时,我总是在去那里时找不到404

web.go文件基本上是https://github.com/yvasiyarov/swagger/blob/master/web.go-example。它基本上是repo中web.go-example的副本......我不明白为什么会返回404.

您对类似的api文档生成器有什么建议吗?我不想要像https://github.com/go-swagger/go-swagger这样的代码生成器。我想根据现有代码

生成api

以下是生成的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": ""
                }
            }
        }
    }
}`,}

2 个答案:

答案 0 :(得分:0)

转到您拥有docs.goweb.go的目录,并假设您使用swagger下载了go get github.com/yvasiyarov/swagger包,以下内容适用于您:

$ go run web.go docs.go --staticPath=$GOPATH/src/github.com/yvasiyarov/swagger/swagger-ui --host=127.0.0.1 --port=3000

所以基本上,这里发生了一些事情。如果您查看web.go-example:https://github.com/yvasiyarov/swagger/blob/master/web.go-example#L14-L17,您可以看到它使用了一些默认设置,这些默认设置只能在您的本地环境中运行,因此您可以执行以下两项操作之一:

1 - 每次都运行上面显示的命令,因此您需要指定swagger-ui路径,以及要为其提供服务的hostport

2 - 修改web.go以使其采用默认配置,您只需运行go run web.go docs.go即可。根据上面的命令,更改将如下所示:

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")

然后转到http://127.0.0.1:3000/swagger-ui/并享受乐趣!

注意:如果您的swagger来源位于其他地方(不在您的$GOPATH中,请确保使用该路线。即

1 - 在你的命令中:

$ go run ... -staticPath=/some/path/.../swagger/swagger-ui

2 - 或在web.go

var staticContent = flag.String("staticPath", "/some/path/.../swagger/swagger-ui", "Path to folder with Swagger UI")

答案 1 :(得分:0)

对于它的价值,swagger也支持代码生成的反转。 您可以在此处找到相关文档:https://goswagger.io/generate/spec.html