当前位置:网站首页>Super efficient! The secret of swagger Yapi
Super efficient! The secret of swagger Yapi
2022-07-05 12:48:00 【InfoQ】

One 、Swagger brief introduction
Two 、Swagger build


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
- This part is all about swagger Usage of , Let's start with the simplest , Interface notes , Copy the comments above the sample to the top of the interface :


- Sample document comments :

- function swagger, Generate interface document
- command :swagger generate spec -o ./swagger.json

- start-up swagger service , Enter the interface document page
- command :swagger serve --no-open swagger.json

3、 ... and 、Swagger standard
// Go-Swagger API.(title)
//
// This is our test 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 Test interface
//
// be used for Swagger test
//
// 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 Interface test parameters 1 (description)
// required: true( Whether must )
// in: query( Where the parameter is located )
ID uinat64 `json:"id"`
}

- POST:
// swagger:parameters swagger_test_checkout
type SwaggerTest struct {
// SwaggerTest Interface test parameters 1 (description)
// required: true( Whether must )
// in: formData( Where the parameter is located )
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 Test interface
//
// be used for Swagger test
//
// 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 Test interface
//
// be used for Swagger test
//
// 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 Test interface
//
// be used for Swagger test
//
// 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
Four 、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


5、 ... and 、 Conclusion
边栏推荐
- Distributed solution - distributed session consistency problem
- Take you hand in hand to develop a service monitoring component
- Distributed solution - completely solve website cross domain requests
- Leetcode-1. Sum of two numbers (Application of hash table)
- ZABBIX agent2 installation
- Time conversion error
- Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
- NFT: how to make money with unique assets?
- Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
- Cypher syntax of neo4j graph database
猜你喜欢

Taobao short videos are automatically released in batches without manual RPA open source

Pytoch loads the initialization V3 pre training model and reports an error

2021-12-21 transaction record

Compilation principle reading notes (1/12)

Interviewer: is acid fully guaranteed for redis transactions?

Third party payment interface design

Summary of C language learning problems (VS)

Solve the problem of cache and database double write data consistency

Detailed structure and code of inception V3

HiEngine:可媲美本地的云原生内存数据库引擎
随机推荐
GNN(pytorch-geometric)
激动人心!2022开放原子全球开源峰会报名火热开启!
Wechat enterprise payment to change access, open quickly
MySQL log module of InnoDB engine
Tips and tricks of image segmentation summarized from 39 Kabul competitions
Summary of C language learning problems (VS)
How can labels/legends be added for all chart types in chart. js (chartjs.org)?
Interviewer: is acid fully guaranteed for redis transactions?
太方便了,钉钉上就可完成代码发布审批啦!
Principle of universal gbase high availability synchronization tool in Nanjing University
Learning JVM garbage collection 06 - memory set and card table (hotspot)
JDBC -- use JDBC connection to operate MySQL database
GPON other manufacturers' configuration process analysis
Keras implements verification code identification
Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
Solution to order timeout unpaid
How to recover the information server and how to recover the server data [easy to understand]
2021.12.16-2021.12.20 empty four hand transaction records
Introduction to GNN
ZABBIX monitors mongodb (template and deployment operations)