0

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

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": ""
                }
            }
        }
    }
}`,}
4

2 回答 2

0

对于它的价值 go-swagger 还支持代码生成的逆向。你可以在这里找到它的文档:https ://goswagger.io/generate/spec.html

于 2016-10-17T05:29:55.677 回答
0

转到您拥有docs.go和的目录web.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路径,以及您想要提供它的host位置port

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")
于 2016-10-17T04:37:11.200 回答