当前位置:网站首页>[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 !
边栏推荐
- Right click menu of QT
- Multi version concurrency control mvcc of MySQL
- Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
- Tensorflow2 keras 分类模型
- Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
- There is a problem with MySQL installation (the service already exists)
- Judge whether it is Sudoku
- Analysis and solution of a classical Joseph problem
- NPOI 导出Word 字号对应
- Redis安装部署(Windows/Linux)
猜你喜欢
【Go实战基础】gin 如何绑定与使用 url 参数
Talk about the secret of high performance of message queue -- zero copy technology
Kubernetes deploys Loki logging system
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Installing Oracle database 19C for Linux
WSL installation, beautification, network agent and remote development
Linux二进制安装Oracle Database 19c
《统计学习方法》——第五章、决策树模型与学习(上)
How to realize asynchronous programming in a synchronous way?
查看was发布的应用程序的端口
随机推荐
《统计学习方法》——第五章、决策树模型与学习(上)
MYSQL安装出现问题(The service already exists)
Qt QTimer类
C4D quick start tutorial - C4d mapping
[go practical basis] how to bind and use URL parameters in gin
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
【Go实战基础】gin 高效神器,如何将参数绑定到结构体
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
Synchronize files using unison
Web技术发展史
Connect function and disconnect function of QT
Avoid breaking changes caused by modifying constructor input parameters
Leetcode sword finger offer brush questions - day 23
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
Minecraft install resource pack
Matplotlib剑客行——初相识Matplotlib
[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)
Redis zadd导致的一次线上问题排查和处理
There is a problem with MySQL installation (the service already exists)
Npoi export word font size correspondence