当前位置:网站首页>超高效!Swagger-Yapi的秘密
超高效!Swagger-Yapi的秘密
2022-07-05 12:34:00 【InfoQ】

一、Swagger简介
二、Swagger搭建


mkdir DownLoad
cd DownLoad
git clone https://github.com/go-swagger/go-swagger
cd DownLoad/go-swagger-master/cmd/swagger/
go install .
[[email protected] /]$ swagger -h
Usage:
swagger [OPTIONS] <command>
Swagger tries to support you as best as possible when building APIs.
It aims to represent the contract of your API with a language agnostic description of your application in json or yaml.
Application Options:
-q, --quiet silence logs
--log-output=LOG-FILE redirect logs to file
Help Options:
-h, --help Show this help message
Available commands:
diff diff swagger documents
expand expand $ref fields in a swagger spec
flatten flattens a swagger document
generate generate go code
init initialize a spec document
mixin merge swagger documents
serve serve spec and docs
validate validate the swagger document
version print the version
- 这一部分全部都是关于swagger的用法的,先来个最简单的,接口注释,把样例上面的注释放复制到接口上方:


- 样例文档注释:

- 运行swagger,生成接口文档
- 命令:swagger generate spec -o ./swagger.json

- 启动swagger服务,进入接口文档页面
- 命令:swagger serve --no-open swagger.json

三、Swagger规范
// Go-Swagger API.(title)
//
// 这是我们的测试API (description)
//
// 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: /go-swagger/test
// Version: 0.0.1
// License: MIT http://opensource.org/licenses/MIT
// Contact: Zhubangzheng<[email protected]> [email protected]
//
// Consumes:
// - application/json
// - application/xml
//
// Produces:
// - application/json
// - application/xml
//
// Security:
// - api_key:
//
// SecurityDefinitions:
// api_key:
// type: apiKey
// name: KEY
// in: header
// oauth2:
// type: oauth2
// authorizationUrl: /oauth2/auth
// tokenUrl: /oauth2/token
// in: header
// scopes:
// bar: foo
// flow: accessCode
//
// Extensions:
// x-meta-value: value
// x-meta-array:
// - value1
// - value2
// x-meta-array-obj:
// - name: obj
// value: field
//
// swagger:meta
package test

// ServeAPI serves the API for this record store
func ServeAPI(host, basePath string, schemes []string) error {
// swagger:route GET /{id}/checkout SwaggerTest swagger_test_checkout
//
// Swagger测试接口
//
// 用于Swagger测试
//
// Consumes:
// - application/json
// - application/x-protobuf
//
// Produces:
// - application/json
// - application/x-protobuf
//
// Schemes: http, https, ws, wss
//
// Deprecated: true
//
// Security:
// api_key:
// oauth: read, write
//
// Responses:
// default: genericError
// 200: someResponse
// 422: validationError
mountItem("GET", basePath+"/{id}/checkout", nil)
}

// swagger:parameters swagger_test_checkout
type SwaggerTest struct {
// SwaggerTest接口测试参数1 (description)
// required: true(是否必须)
// in: query(参数所在的位置)
ID uinat64 `json:"id"`
}

- POST:
// swagger:parameters swagger_test_checkout
type SwaggerTest struct {
// SwaggerTest接口测试参数1 (description)
// required: true(是否必须)
// in: formData(参数所在的位置)
ID uinat64 `json:"id"`
}
// ServeAPI serves the API for this record store
func ServeAPI(host, basePath string, schemes []string) error {
// swagger:route GET /{id}/checkout SwaggerTest swagger_test_checkout
//
// Swagger测试接口
//
// 用于Swagger测试
//
// Consumes:
// multipart/form-data
//
// ......
mountItem("GET", basePath+"/{id}/checkout", nil)
}
// A ValidationError is an error that is used when the required input fails validation.
// swagger:response validationError
type ValidationError struct {
// The error message
// in: body
Body struct {
// The validation message
//
// Required: true
// Example: Expected type int
Message string
// An optional field name to which this validation applies
FieldName string
}
}
// SwaggerTestResponse
// swagger:response test_res
type SwaggerTestResponse struct {
// The error message
// in: body
Body struct {
// The validation message
//
// Required: true
// Example: Expected type int
Message string
// An optional field name to which this validation applies
FieldName string
}
}
// Test
// swagger:response old_api_resp
type OldAPIRes struct {
// Test
// in: body
ID uint64
Name string
Time string
}
// ServeAPI serves the API for this record store
func ServeAPI(host, basePath string, schemes []string) error {
// swagger:route GET /{id}/checkout SwaggerTest swagger_test_checkout
//
// Swagger测试接口
//
// 用于Swagger测试
//
// Consumes:
// - multipart/form-data
// Schemes: http
// Responses:
// 200: old_api_resp
mountItem("GET", basePath+"/{id}/checkout", nil)
}

// Test
// swagger:response old_api_resp
type OldAPIRes struct {
// Test
// in: body
Body struct {
ID uint64
Name string
Time string
}
}
// swagger:model old_api_resp
type OldAPIRes struct {
ID uint64
Name string
Time string
}
// ServeAPI serves the API for this record store
func ServeAPI(host, basePath string, schemes []string) error {
// swagger:route GET /{id}/checkout SwaggerTest swagger_test_checkout
//
// Swagger测试接口
//
// 用于Swagger测试
//
// Consumes:
// - multipart/form-data
// Produces:
// - application/json
// Schemes: http
// Responses:
// 200: body:old_api_resp
mountItem("GET", basePath+"/{id}/checkout", nil)
swagger generate spec -m -o ./swagger.json
四、Swagger-Yapi



sudo yum install nginx -y
rpm -qa | grep nginx
sudo systemctl start nginx
sudo service nginx start
sudo systemctl status nginx
sudo service nginx status
cd /etc/nginx/
cd conf.d/
vim yapi.conf
server {
listen 8888;
server_name localhost;
location /data/ {
alias '/home/work/Swagger/swagger-yapi/swagger-json/';
}
}
sudo systemctl restart nginx
sudo service nginx restart
hostname -i


五、结语
边栏推荐
- Add a new cloud disk to Huawei virtual machine
- UNIX socket advanced learning diary - advanced i/o functions
- ZABBIX monitors mongodb (template and deployment operations)
- Learning items
- Learn the garbage collector of JVM -- a brief introduction to Shenandoah collector
- Get all stock data of big a
- Introduction to relational model theory
- Constructing expression binary tree with prefix expression
- Database connection pool & jdbctemplate
- The evolution of mobile cross platform technology
猜你喜欢
Understand redis persistence mechanism in one article
Anaconda creates a virtual environment and installs pytorch
Resnet18 actual battle Baoke dream spirit
Detailed structure and code of inception V3
Resnet+attention project complete code learning
One article tells the latest and complete learning materials of flutter
VoneDAO破解组织发展效能难题
Add a new cloud disk to Huawei virtual machine
Redis highly available sentinel cluster
CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器
随机推荐
Master the new features of fluent 2.10
在家庭智能照明中应用的测距传感芯片4530A
Average lookup length when hash table lookup fails
Simple production of wechat applet cloud development authorization login
Take you hand in hand to develop a service monitoring component
只是巧合?苹果 iOS16 的神秘技术竟然与中国企业 5 年前产品一致!
Redis highly available sentinel mechanism
Time conversion error
How to recover the information server and how to recover the server data [easy to understand]
[superhard core] is the core technology of redis
MySQL view
Tabbar configuration at the bottom of wechat applet
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Storage Basics
JDBC -- use JDBC connection to operate MySQL database
Pytorch two-layer loop to realize the segmentation of large pictures
Learn memory management of JVM 01 - first memory
VoneDAO破解组织发展效能难题
CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器
struct MySQL