2016-10-17 7 views
0

サンプルプロジェクトを使用してこのgithubページhttps://github.com/yvasiyarov/swaggerの手順をたどり、apiドキュメントが正常に生成されました。しかし、APIページを実行して表示する最後のコマンドを実行すると、私はそこに行くと常に404が見つかりません。Golang swagger return 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.goweb.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": "[email protected]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": "" 
       } 
      } 
     } 
    } 
}`,} 
+0

あなたは本当にいくつかの大きな文字列だけでなく、実際のコードを表示する必要があります。 – Volker

答えて

0

ゴーdocs.go生成され、そしてあなたがgo get github.com/yvasiyarov/swaggerを使用してswaggerパッケージをダウンロードしたと仮定されて生成したいです次のようにしてください:

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

基本的に、ここでは2つのことが起こっています。 web.go-example:https://github.com/yvasiyarov/swagger/blob/master/web.go-example#L14-L17を調べると、それはあなたのローカル環境ではうまくいかない、いくつかのデフォルトを使用していることがわかります。

1 - showコマンドを実行します毎回上記のように、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/に行くと楽しい時を過します!

注:あなたはどこか別の場所(ないあなたの$GOPATHで、ちょうどあなたがそのルートを使用してくださいあなたのswaggerソースを持っている場合、すなわち

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

あなたの価値があるものは、コード生成の ここにそのドキュメントを見つけることができます:https://goswagger.io/generate/spec.html

+0

私は実際にこれを調べ始めました。それは良いと思われるが、セットアップを行うために甘やかすことは難しく、あいまいである。私は仕様を生成しようとしましたが、基本的に '{" swagger ":" 2.0 "、" paths ":{}、" definitions ":{}}'を出力します。私はmain.goと同じディレクトリでそれをやっていて、main.goファイルの上に 'go:generate'の注釈があります。これをより詳細に説明する良いチュートリアルやウィキを知っていますか? – Sakib

関連する問題