2016-02-26 25 views
0

這裏是我的簡單的REST服務:細末招搖不產生模型信息

// Package classification User API. 
// 
// the purpose of this application is to provide an application 
// that is using plain go code to define an API 
// 
// This should demonstrate all the possible comment annotations 
// that are available to turn go code into a fully compliant swagger 2.0 spec 
// 
// Terms Of Service: 
// 
// there are no TOS at this moment, use at your own risk we take no responsibility 
// 
//  Schemes: http, https 
//  Host: localhost 
//  BasePath: /v2 
//  Version: 0.0.1 
//  License: MIT http://opensource.org/licenses/MIT 
//  Contact: John Doe<[email protected]> http://john.doe.com 
// 
//  Consumes: 
//  - application/json 
//  - application/xml 
// 
//  Produces: 
//  - application/json 
//  - application/xml 
// 
// 
// swagger:meta 
package main 
import (
"github.com/gin-gonic/gin" 
"strconv" 
"database/sql" 
_ "github.com/go-sql-driver/mysql" 
"gopkg.in/gorp.v1" 
"log" 
) 

// swagger:model 
// User represents the user for this application 
// 
// A user is the security principal for this application. 
// It's also used as one of main axis for reporting. 
// 
// A user can have friends with whom they can share what they like. 
// 
type User struct { 
    // the id for this user 
    // 
    // required: true 
    // min: 1 
    Id int64 `db:"id" json:"id"` 
    // the first name for this user 
    // required: true 
    // min length: 3 
    Firstname string `db:"firstname" json:"firstname"` 
    // the last name for this user 
    // required: true 
    // min length: 3 
    Lastname string `db:"lastname" json:"lastname"` 
} 

func main() { 
r := gin.Default() 
r.Use(Cors()) 
v1 := r.Group("api/v1") 
{ 
v1.GET("/users", GetUsers) 
v1.GET("https://stackoverflow.com/users/:id", GetUser) 
v1.POST("/users", PostUser) 
v1.PUT("https://stackoverflow.com/users/:id", UpdateUser) 
v1.DELETE("https://stackoverflow.com/users/:id", DeleteUser) 
v1.OPTIONS("/users", OptionsUser)  // POST 
v1.OPTIONS("https://stackoverflow.com/users/:id", OptionsUser) // PUT, DELETE 
} 
r.Run(":8696") 
} 


func GetUsers(c *gin.Context) { 
    // swagger:route GET /user listPets pets users 
    // 
    // Lists pets filtered by some parameters. 
    // 
    // This will show all available pets by default. 
    // You can get the pets that are out of stock 
    // 
    //  Consumes: 
    //  - application/json 
    //  - application/x-protobuf 
    // 
    //  Produces: 
    //  - application/json 
    //  - application/x-protobuf 
    // 
    //  Schemes: http, https, ws, wss 
    // 
    //  Security: 
    //  api_key: 
    //  oauth: read, write 
    // 
    //  Responses: 
    //  default: genericError 
    //  200: someResponse 
    //  422: validationError 
    var users []User 
    _, err := dbmap.Select(&users, "SELECT * FROM user") 
    if err == nil { 
    c.JSON(200, users) 
    } else { 
    c.JSON(404, gin.H{"error": "no user(s) into the table"}) 
} 
// curl -i http://localhost:8080/api/v1/users 
} 

func GetUser(c *gin.Context) { 
id := c.Params.ByName("id") 
var user User 
err := dbmap.SelectOne(&user, "SELECT * FROM user WHERE id=?", id) 
if err == nil { 
user_id, _ := strconv.ParseInt(id, 0, 64) 
content := &User{ 
Id: user_id, 
Firstname: user.Firstname, 
Lastname: user.Lastname, 
} 
c.JSON(200, content) 
} else { 
c.JSON(404, gin.H{"error": "user not found"}) 
} 
// curl -i http://localhost:8080/api/v1/users/1 
} 

func PostUser(c *gin.Context) { 
var user User 
c.Bind(&user) 
if user.Firstname != "" && user.Lastname != "" { 
if insert, _ := dbmap.Exec(`INSERT INTO user (firstname, lastname) VALUES (?, ?)`, user.Firstname, user.Lastname); insert != nil { 
user_id, err := insert.LastInsertId() 
if err == nil { 
content := &User{ 
Id: user_id, 
Firstname: user.Firstname, 
Lastname: user.Lastname, 
} 
c.JSON(201, content) 
} else { 
checkErr(err, "Insert failed") 
} 
} 
} else { 
c.JSON(422, gin.H{"error": "fields are empty"}) 
} 
// curl -i -X POST -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Queen\" }" http://localhost:8080/api/v1/users 
} 

func UpdateUser(c *gin.Context) { 
id := c.Params.ByName("id") 
var user User 
err := dbmap.SelectOne(&user, "SELECT * FROM user WHERE id=?", id) 
if err == nil { 
var json User 
c.Bind(&json) 
user_id, _ := strconv.ParseInt(id, 0, 64) 
user := User{ 
Id: user_id, 
Firstname: json.Firstname, 
Lastname: json.Lastname, 
} 
if user.Firstname != "" && user.Lastname != ""{ 
_, err = dbmap.Update(&user) 
if err == nil { 
c.JSON(200, user) 
} else { 
checkErr(err, "Updated failed") 
} 
} else { 
c.JSON(422, gin.H{"error": "fields are empty"}) 
} 
} else { 
c.JSON(404, gin.H{"error": "user not found"}) 
} 
// curl -i -X PUT -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Merlyn\" }" http://localhost:8080/api/v1/users/1 
} 

func DeleteUser(c *gin.Context) { 
id := c.Params.ByName("id") 
var user User 
err := dbmap.SelectOne(&user, "SELECT id FROM user WHERE id=?", id) 
if err == nil { 
_, err = dbmap.Delete(&user) 
if err == nil { 
c.JSON(200, gin.H{"id #" + id: " deleted"}) 
} else { 
checkErr(err, "Delete failed") 
} 
} else { 
c.JSON(404, gin.H{"error": "user not found"}) 
} 
// curl -i -X DELETE http://localhost:8080/api/v1/users/1 
} 

var dbmap = initDb() 
func initDb() *gorp.DbMap { 
db, err := sql.Open("mysql", 
     "root:[email protected](127.0.0.1:3306)/gotest") 
checkErr(err, "sql.Open failed") 
dbmap := &gorp.DbMap{Db: db, Dialect:   gorp.MySQLDialect{"InnoDB", "UTF8"}} 
dbmap.AddTableWithName(User{}, "User").SetKeys(true, "Id") 
err = dbmap.CreateTablesIfNotExists() 
checkErr(err, "Create table failed") 
return dbmap 
} 

func checkErr(err error, msg string) { 
if err != nil { 
log.Fatalln(msg, err) 
} 
} 


func Cors() gin.HandlerFunc { 
return func(c *gin.Context) { 
c.Writer.Header().Add("Access-Control-Allow-Origin", "*") 
c.Next() 
} 
} 

func OptionsUser(c *gin.Context) { 
c.Writer.Header().Add("Access-Control-Allow-Origin", "*") 
c.Writer.Header().Set("Access-Control-Allow-Methods", "DELETE,POST, PUT") 
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type") 
c.Next() 
} 

現在,當我執行: 招搖生成規範-o ./swagger.json 生成JSON規範我m得到:

{ 
    "consumes": ["application/json", "application/xml"], 
    "produces": ["application/json", "application/xml"], 
    "schemes": ["http", "https"], 
    "swagger": "2.0", 
    "info": { 
     "description": "the purpose of this application is to provide an application\nthat is using plain go code to define an API\n\nThis should demonstrate all the possible comment annotations\nthat are available to turn go code into a fully compliant swagger 2.0 spec", 
     "title": "User API.", 
     "termsOfService": "there are no TOS at this moment, use at your own risk we take no responsibility", 
     "contact": { 
      "name": "John Doe", 
      "url": "http://john.doe.com", 
      "email": "[email protected]" 
     }, 
     "license": { 
      "name": "MIT", 
      "url": "http://opensource.org/licenses/MIT" 
     }, 
     "version": "0.0.1" 
    }, 
    "host": "localhost", 
    "basePath": "/v2", 
    "paths": { 
     "/user": { 
      "get": { 
       "description": "This will show all available pets by default.\nYou can get the pets that are out of stock", 
       "consumes": ["application/json", "application/x-protobuf"], 
       "produces": ["application/json", "application/x-protobuf"], 
       "schemes": ["http", "https", "ws", "wss"], 
       "tags": ["listPets", "pets"], 
       "summary": "Lists pets filtered by some parameters.", 
       "operationId": "users", 
       "security": [{ 
        "api_key": null 
       }, { 
        "oauth": ["read", "write"] 
       }], 
       "responses": { 
        "200": { 
         "$ref": "#/responses/someResponse" 
        }, 
        "422": { 
         "$ref": "#/responses/validationError" 
        }, 
        "default": { 
         "$ref": "#/responses/genericError" 
        } 
       } 
      } 
     } 
    }, 
    "definitions": {} 
} 

請注意,我的定義是空的,不知道爲什麼。 如果我粘貼http://editor.swagger.io/#/ 相同的JSON規範它說

Error 
Object 
message: "options.definition is required" 
code: "UNCAUGHT_SWAY_WORKER_ERROR" 

什麼是產生招搖文件的正確方法的任何方向將有助於

回答

0

這是因爲去-招搖無法檢測的使用你的定義。我假設你總是有一個描述參數的結構,並且它們總是使用正在使用的定義。 如果您可以使用您的示例程序將此問題作爲回購問題提交,那將是非常好的。我會將其添加到我的測試中。

+0

issue#334謝謝! – Max