Skip to content

Commit

Permalink
Merge pull request pangpanglabs#32 from elvinchan/31
Browse files Browse the repository at this point in the history
pangpanglabs#31 Fix docs & update swagger-ui version
  • Loading branch information
elvinchan authored Dec 30, 2019
2 parents 755b47a + 12628a8 commit dfc2a3d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
English | [简体中文](./README_zh-CN.md)

# Echoswagger
Swagger UI generator for Echo framework
[Swagger UI](https://github.com/swagger-api/swagger-ui) generator for [Echo](https://github.com/labstack/echo) framework

[![Go Report Card](https://goreportcard.com/badge/github.com/pangpanglabs/echoswagger)](https://goreportcard.com/report/github.com/pangpanglabs/echoswagger)
[![Build Status](https://travis-ci.org/pangpanglabs/echoswagger.svg?branch=master)](https://travis-ci.org/pangpanglabs/echoswagger)
Expand Down Expand Up @@ -55,55 +55,55 @@ func createUser(c echo.Context) error {

## Usage
#### Create a `ApiRoot` with `New()`, which is a wrapper of `echo.New()`
```
```go
r := echoswagger.New(echo.New(), "/doc", nil)
```
You can use the result `ApiRoot` instance to:
- Setup Security definitions, request/response Content-Types, UI options, Scheme, etc.
```
```go
r.AddSecurityAPIKey("JWT", "JWT Token", echoswagger.SecurityInHeader).
SetRequestContentType("application/x-www-form-urlencoded", "multipart/form-data").
SetUI(UISetting{HideTop: true}).
SetScheme("https", "http")
```
- Get `echo.Echo` instance.
```
```go
r.Echo()
```
- Registers a new GET, POST, PUT, DELETE, OPTIONS, HEAD or PATCH route in default group, these are wrappers of Echo's create route methods.
It returns a new `Api` instance.
```
```go
r.GET("/:id", handler)
```
- And: ↓

#### Create a `ApiGroup` with `Group()`, which is a wrapper of `echo.Group()`
```
```go
g := r.Group("Users", "/users")
```
You can use the result `ApiGroup` instance to:
- Set description, etc.
```
```go
g.SetDescription("The desc of group")
```
- Set security for all routes in this group.
```
```go
g.SetSecurity("JWT")
```
- Get `echo.Group` instance.
```
```go
g.EchoGroup()
```
- And: ↓

#### Registers a new route in `ApiGroup`
GET, POST, PUT, DELETE, OPTIONS, HEAD or PATCH methods are supported by Echoswagger, these are wrappers of Echo's create route methods.
```
```go
a := g.GET("/:id", handler)
```
You can use the result `Api` instance to:
- Add parameter with these methods:
```
```go
AddParamPath(p interface{}, name, desc string)

AddParamPathNested(p interface{})
Expand All @@ -128,37 +128,37 @@ AddParamFile(name, desc string, required bool)
The methods which name's suffix are `Nested` means these methods treat parameter `p` 's fields as paramters, so it must be a struct type.

e.g.
```
```go
type SearchInput struct {
Q string `query:"q" swagger:"desc(Keywords),required"`
SkipCount int `query:"skipCount"`
}
a.AddParamQueryNested(SearchInput{})
```
Is equivalent to:
```
```go
a.AddParamQuery("", "q", "Keywords", true).
AddParamQuery(0, "skipCount", "", false)
```
- Add responses.
```
```go
a.AddResponse(http.StatusOK, "response desc", body{}, nil)
```
- Set Security, request/response Content-Types, summary, description, etc.
```
```go
a.SetSecurity("JWT").
SetResponseContentType("application/xml").
SetSummary("The summary of API").
SetDescription("The desc of API")
```
- Get `echo.Route` instance.
```
```go
a.Route()
```

#### With swagger tag, you can set more info with `AddParam...` methods.
#### With `swagger` tag, you can set more info with `AddParam...` methods.
e.g.
```
```go
type User struct {
Age int `swagger:"min(0),max(99)"`
Gender string `swagger:"enum(male|female|other),required"`
Expand All @@ -167,7 +167,7 @@ type User struct {
a.AddParamBody(&User{}, "Body", "", true)
```
The definition is equivalent to:
```
```json
{
"definitions": {
"User": {
Expand Down Expand Up @@ -206,15 +206,15 @@ The definition is equivalent to:
}
```

**Supported swagger tags:**
**Supported `swagger` tags:**

Tag | Type | Description
---|:---:|---
desc | `string` | Description.
maximum | `number` | -
minimum | `number` | -
maxLength | `integer` | -
minLength | `integer` | -
min | `number` | -
max | `number` | -
minLen | `integer` | -
maxLen | `integer` | -
allowEmpty | `boolean` | Sets the ability to pass empty-valued parameters. This is valid only for either `query` or `formData` parameters and allows you to send a parameter with a name only or an empty value. Default value is `false`.
required | `boolean` | Determines whether this parameter is mandatory. If the parameter is `in` "path", this property is `true` without setting. Otherwise, the property MAY be included and its default value is `false`.
readOnly | `boolean` | Relevant only for Schema `"properties"` definitions. Declares the property as "read only". This means that it MAY be sent as part of a response but MUST NOT be sent as part of the request. Properties marked as `readOnly` being `true` SHOULD NOT be in the `required` list of the defined schema. Default value is `false`.
Expand Down
46 changes: 23 additions & 23 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ package main
import (
"net/http"

"github.com/pangpanglabs/echoswagger"
"github.com/labstack/echo"
"github.com/pangpanglabs/echoswagger"
)

func main() {
Expand Down Expand Up @@ -55,55 +55,55 @@ func createUser(c echo.Context) error {

## 用法
#### `New()`创建`ApiRoot`,此方法是对`echo.New()`方法的封装
```
```go
r := echoswagger.New(echo.New(), "/doc", nil)
```
你可以用这个`ApiRoot`来:
- 设置Security定义, 请求/响应Content-Type,UI选项,Scheme等。
```
```go
r.AddSecurityAPIKey("JWT", "JWT Token", echoswagger.SecurityInHeader).
SetRequestContentType("application/x-www-form-urlencoded", "multipart/form-data").
SetUI(UISetting{HideTop: true}).
SetScheme("https", "http")
```
- 获取`echo.Echo`实例。
```
```go
r.Echo()
```
- 在默认组中注册一个GET、POST、PUT、DELETE、OPTIONS、HEAD或PATCH路由,这些是对Echo的注册路由方法的封装。
此方法返回一个`Api`实例。
```
```go
r.GET("/:id", handler)
```
- 以及: ↓

#### `Group()`创建`ApiGroup`,此方法是对`echo.Group()`方法的封装
```
```go
g := r.Group("Users", "/users")
```
你可以用这个`ApiGroup`来:
- 设置描述,等。
```
```go
g.SetDescription("The desc of group")
```
- 为此组中的所有路由设置Security。
```
```go
g.SetSecurity("JWT")
```
- 获取`echo.Group`实例。
```
```go
g.EchoGroup()
```
- 以及: ↓

#### `ApiGroup`中注册一个新的路由
Echoswagger支持GET、POST、PUT、DELETE、OPTIONS、HEAD或PATCH方法,这些是对Echo的注册路由方法的封装。
```
```go
a := g.GET("/:id", handler)
```
你可以使用此`Api`实例来:
- 使用以下方法添加参数:
```
```go
AddParamPath(p interface{}, name, desc string)

AddParamPathNested(p interface{})
Expand All @@ -128,37 +128,37 @@ AddParamFile(name, desc string, required bool)
后缀带有`Nested`的方法把参数`p`的字段看做多个参数,所以它必须是结构体类型的。

例:
```
```go
type SearchInput struct {
Q string `query:"q" swagger:"desc(Keywords),required"`
SkipCount int `query:"skipCount"`
}
a.AddParamQueryNested(SearchInput{})
```
等价于:
```
```go
a.AddParamQuery("", "q", "Keywords", true).
AddParamQuery(0, "skipCount", "", false)
```
- 添加响应。
```
```go
a.AddResponse(http.StatusOK, "response desc", body{}, nil)
```
- 设置Security,请求/响应的Content-Type,概要,描述,等。
```
```go
a.SetSecurity("JWT").
SetResponseContentType("application/xml").
SetSummary("The summary of API").
SetDescription("The desc of API")
```
- 获取`echo.Route`实例。
```
```go
a.Route()
```

#### 使用swagger标签,你可以在`AddParam...`方法中设置更多信息
#### 使用`swagger`标签,你可以在`AddParam...`方法中设置更多信息
例:
```
```go
type User struct {
Age int `swagger:"min(0),max(99)"`
Gender string `swagger:"enum(male|female|other),required"`
Expand All @@ -167,7 +167,7 @@ type User struct {
a.AddParamBody(&User{}, "Body", "", true)
```
此定义等价于:
```
```json
{
"definitions": {
"User": {
Expand Down Expand Up @@ -211,10 +211,10 @@ a.AddParamBody(&User{}, "Body", "", true)
Tag | Type | Description
---|:---:|---
desc | `string` | 描述。
maximum | `number` | -
minimum | `number` | -
maxLength | `integer` | -
minLength | `integer` | -
min | `number` | -
max | `number` | -
minLen | `integer` | -
maxLen | `integer` | -
allowEmpty | `boolean` | 设置传递空值参数的功能。 这仅对`query``formData`参数有效,并允许你发送仅具有名称或空值的参数。默认值为“false”。
required | `boolean` | 确定此参数是否必需。如果参数是`in`“path”,则此属性默认为“true”。否则,可以设置此属性,其默认值为“false”。
readOnly | `boolean` | 仅与Schema`"properties"`定义相关。将属性声明为“只读”。这意味着它可以作为响应的一部分发送,但绝不能作为请求的一部分发送。标记为“readOnly”的属性为“true”,不应位于已定义模式的“required”列表中。默认值为“false”。
Expand Down
4 changes: 2 additions & 2 deletions assets.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package echoswagger

// CDN refer to https://www.jsdelivr.com/package/npm/swagger-ui-dist
const DefaultCDN = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.22.1"
const DefaultCDN = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.24.3"

const SwaggerUIContent = `{{define "swagger"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{.title}}</title>
<link rel="stylesheet" href="{{.cdn}}/swagger-ui.css" crossorigin="anonymous" />
<link rel="stylesheet" type="text/css" href="{{.cdn}}/swagger-ui.css" />
<link rel="icon" type="image/png" href="{{.cdn}}/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="{{.cdn}}/favicon-16x16.png" sizes="16x16" />
<style>
Expand Down

0 comments on commit dfc2a3d

Please sign in to comment.