当前位置:网站首页>[go practical basis] how to customize and use a middleware in gin
[go practical basis] how to customize and use a middleware in gin
2022-07-02 09:08:00 【Novice practice】
Catalog
3、 ... and 、 Rookie actual combat
add to get Routing and callback
One 、 brief introduction
Actual combat scene : gin How to customize and use a middleware
Two 、 Knowledge point
gin route
Array of strings
Array loop
Custom middleware
http Status code
3、 ... and 、 Rookie actual combat
Make arrangements now !
1、 establish go file
/*
* @Author: Rookie actual combat
* @Description: gin How to customize and use a middleware
*/
// Knowledge point :
// # gin route
// # Array of strings
// # Array loop
// # Custom middleware
// # http Status code
package main
// Import package
import (
"fmt"
"github.com/gin-gonic/gin"
"runtime"
)
// IP Authorization middleware
func IPAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// White list
whiteIpList := []string{
"127.0.0.1",
}
flag := false
// Get the current access ip, And white list ip compare
clientIp := c.ClientIP()
fmt.Printf(" At present client ip by : %s \n", clientIp)
for _, whiteIp := range whiteIpList {
// Verification passed
if clientIp == whiteIp {
flag = true
break
}
}
// Verification failed
if flag == false {
c.String(401, "client ip % is not in the white ip list", clientIp)
// need abort
c.Abort()
}
}
}
// The main function
func main() {
// Print using built-in functions
println("Hello", " Rookie actual combat ")
println(" Actual combat scene : ", "gin How to customize and use a middleware ")
// initialization
r := gin.Default()
// Using middleware
r.Use(IPAuthMiddleware())
// add to get Routing and callback
r.GET("/g", func(c *gin.Context) {
// Back to code and String returns
c.String(200, " This is a method using middleware \n")
})
// Use package functions to print
fmt.Printf(" edition : %s \n", runtime.Version())
// Start the framework program , Default 8080 port
r.Run()
}
// curl Verification mode
// bind get
// curl -X GET "http://127.0.0.1:8080/g"
2、 Running results
Hello Rookie actual combat
Actual combat scene : gin How to customize and use a middleware
[GIN-debug] Listening and serving HTTP on :8080
add to get Routing and callback
Rookie actual combat , Continuous learning !
边栏推荐
- C4D quick start tutorial - C4d mapping
- Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
- 机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
- gocv边界填充
- C language - Blue Bridge Cup - 7 segment code
- 双非本科生进大厂,而我还在底层默默地爬树(上)
- Programmers with ten years of development experience tell you, what core competitiveness do you lack?
- Essay: RGB image color separation (with code)
- Using recursive functions to solve the inverse problem of strings
- QT drag event
猜你喜欢
【Go实战基础】如何安装和使用 gin
[go practical basis] how to set the route in gin
kubernetes部署loki日志系统
Avoid breaking changes caused by modifying constructor input parameters
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
C language implementation of mine sweeping game
Finishing the interview essentials of secsha system!!!
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
Introduction to the basic concept of queue and typical application examples
[go practical basis] how can gin get the request parameters of get and post
随机推荐
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
使用递归函数求解字符串的逆置问题
NPOI 导出Word 字号对应
Essay: RGB image color separation (with code)
CSDN Q & A_ Evaluation
gocv opencv exit status 3221225785
libusb的使用
gocv opencv exit status 3221225785
Minecraft air Island service
C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
Image transformation, transpose
kubernetes部署loki日志系统
Move a string of numbers backward in sequence
gocv拆分颜色通道
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
[go practical basis] how to bind and use URL parameters in gin
C language implementation of mine sweeping game
Installing Oracle database 19C RAC on Linux
Gocv image reading and display
寻找链表中值域最小的节点并移到链表的最前面