This is the sixth day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021.
Go-swagger installation and use
Go-swagger brings to the community a complete, full-featured set of high-performance API components that can be used with the Swagger API: servers, clients, and data models.
- Build servers according to standard specifications
- Generate clients from rickety specifications
- Support for most of the functionality provided by jsonschema and swagger, including polymorphism
- Generate standard specifications from annotated GO code
- Other tools used in conjunction with specifications
- Powerful customization features with vendor extensions and customizable templates
- Our focus in code generation is on generating idiomatic, fast-executing code that can be used with Golint, Go Vet, etc
: : :
1 Code Download
Go-swagger source code address download
go get github.com/go-swagger/go-swagger
Copy the code
2 install the swagger
Install the go – swagger, mobile to download the go – swagger package directory (should be GOPATH/src/pkg/github.com/go-swagger, it is good to understand the meaning, path does not necessarily to).
go install ./cmd/swagger
Copy the code
3 swagger specification
Place the following specification in front of each controller function in the GO source code
// swagger:operation POST /user/addUser user addUser
// ---
// summary: Adding User Information
// description: Add system users
// parameters:
// - name: username
// in: body
// description: The user name
// type: string
// required: true
// - name: password
// in: body
// description: password
// type: string
// required: true
// responses:
// 200: repoResp
// 400: badReq
Copy the code
4 Swagger Specification Description
// swagger:operaion [POST: request (can be GET\PUT\DELETE...)] [Url: request address] [The label] [Request for this endpoint] (You can think of the last two as ids Node, used to annotate addresses)
// --- Below this section is the Swagger specification in YAML format. Make sure your indentation is consistent and correct
// summary: The title
// description: describe
// parametres: Here are the parameters
// - name: Parameter names
in: [header|body|query] Position of parameter
description: describe
type: type
required: Whether must
// responses: The response
Copy the code
5 generate swagger. Json
Go to the root directory of your project and create the swagger. Json specification document according to the Swagger specification
swagger generate spec -o ./swagger.json
Copy the code
- -o generate spec -output generate spec -o generate spec -output
- Swagger generates specifications – outputs output paths and files.
6 Start the Swagger service
Start an HTTP service and put the JSON document into petStore.swagger. IO to execute
swagger serve -F=swagger D:\go_project\src\kratos\docs\api.swagger.json
Copy the code
7 Pages about Swagger -UI
If you want a page like petStore.swagger. IO, you can download it yourself at github.com/swagger-api… .
After downloading, go to the Dist page and run index.html
Please refer to the official website for more usage of Go-Swagger