当前位置:网站首页>Gorilla/mux framework (RK boot): add swagger UI
Gorilla/mux framework (RK boot): add swagger UI
2022-06-23 02:22:00 【Trespass 】
Introduce
This article will show you how to gorilla/mux Provided above the framework Swagger UI.
Please visit the following address for complete information gorilla/mux course :
precondition
gorilla/mux No built-in generation Swagger UI The function of configuration file .
We need to install swag Command line tools to generate Swagger UI The configuration file .
Installation options : adopt swag Official website
$ go get -u github.com/swaggo/swag/cmd/swag
install rk-boot
Let's introduce rk-boot library , Users can quickly build gorilla/mux Microservices .
go get github.com/rookie-ninja/rk-boot/mux
Quick start
1. establish boot.yaml
boot.yaml The file will tell rk-boot How to start gorilla/mux service , In the following example , We specified the port ,Swagger UI Of json File path .
---
mux:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
sw:
enabled: true # Optional, default: false
jsonPath: "docs" # Optional, default: ""
# path: "sw" # Default value is "sw", change it as needed
# headers: [] # Headers that will be set while accessing swagger UI main page.2. establish main.go
In order to make swag Command line generation Swagger UI Parameter file , We need to write comments in the code .
Please refer to swag Official documents .
// Copyright (c) 2021 rookie-ninja // // Use of this source code is governed by an Apache-style // license that can be found in the LICENSE file. package main import ( "context" "fmt" "github.com/rookie-ninja/rk-boot" "github.com/rookie-ninja/rk-boot/mux" "github.com/rookie-ninja/rk-mux/interceptor" "net/http" ) // @title Swagger Example API // @version 1.0 // @description This is a sample rk-demo server. // @termsOfService http://swagger.io/terms/ // @securityDefinitions.basic BasicAuth // @contact.name API Support // @contact.url http://www.swagger.io/support // @contact.email [email protected] // @license.name Apache 2.0 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Register handler entry := rkbootmux.GetMuxEntry("greeter") entry.Router.NewRoute().Methods(http.MethodGet).Path("/v1/greeter").HandlerFunc(Greeter) // Bootstrap boot.Bootstrap(context.TODO()) boot.WaitForShutdownSig(context.TODO()) } // @Summary Greeter service // @Id 1 // @version 1.0 // @produce application/json // @Param name query string true "Input name" // @Success 200 {object} GreeterResponse // @Router /v1/greeter [get] func Greeter(writer http.ResponseWriter, request *http.Request) { rkmuxinter.WriteJson(writer, http.StatusOK, &GreeterResponse{ Message: fmt.Sprintf("Hello %s!", request.URL.Query().Get("name")), }) } // Response. type GreeterResponse struct { Message string }
3. Generate swagger Parameter file
The default will be docs Create three files in the folder .rk-boot Will use swagger.json To initialize the Swagger UI Interface .
$ swag init $ tree . ├── boot.yaml ├── docs │ ├── docs.go │ ├── swagger.json │ └── swagger.yaml ├── go.mod ├── go.sum └── main.go 1 directory, 7 files
4. verification
visit :localhost:8080/sw
边栏推荐
- Analysis of web page status code
- Exercise analysis summary
- How to locate memory leaks
- //1.10 initial value of variable
- For Xiaobai who just learned to crawl, you can understand it after reading it
- WM view of commodity master data in SAP retail preliminary level
- Campus network AC authentication failed
- Hello
- How to download online printing on Web pages to local PDF format (manual personal test)
- Array part
猜你喜欢

Deep learning environment configuration (III) pytorch GPU under Anaconda

1. Mx6u bare metal program (2) - Lighting master (imitating 32 register version)

Interviewer: what is the difference between SSH and SSM frameworks? How to choose??

JS rotation chart (Netease cloud rotation chart)

Why is BeanUtils not recommended?

Performance test -- Jenkins environment construction for 15jmeter performance test

2022-1-12

Dynamic address book in C language (add, delete, modify, check (duplicate), delete, sort and export)

JS advanced part

Nfv and SDN
随机推荐
WM view of commodity master data in SAP retail preliminary level
Performance test -- 14 detailed explanation of performance test report and precautions
Dynamic address book in C language (add, delete, modify, check (duplicate), delete, sort and export)
Reptile lesson 1
Three methods for solving Fibonacci sequence feibonacci (seeking rabbit) - program design
1. Mx6u startup mode and equipment
Third order magic cube formula
Deep learning environment configuration (III) pytorch GPU under Anaconda
Detailed explanation of makefile usage
Mongodb aggregate query implements multi table associated query, type conversion, and returns specified parameters.
Pychart installation instructions
Buuctf misc-[actf freshman competition 2020]outline
What is ISBN code and how to make it
Deep scan log4j2 vulnerability using codesec code audit platform
Information theory and coding
C language game minesweeping [simple implementation]
await is only valid in async function
Salesforce fileUpload (I) how to configure the file upload function
Targeted and ready to go
Source code analysis | activity setcontentview I don't flash