当前位置:网站首页>[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 !
边栏推荐
- First week of JS study
- 远程连接IBM MQ报错AMQ4036解决方法
- 分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
- Avoid breaking changes caused by modifying constructor input parameters
- Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
- 整理秒杀系统的面试必备!!!
- 「Redis源码系列」关于源码阅读的学习与思考
- Select sort and insert sort
- CSDN Q & A_ Evaluation
- What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
猜你喜欢

聊聊消息队列高性能的秘密——零拷贝技术

Multi version concurrency control mvcc of MySQL

MYSQL安装出现问题(The service already exists)

WSL安装、美化、网络代理和远程开发
![[go practical basis] how to install and use gin](/img/0d/3e899bf69abf4e8cb7e6a0afa075a9.png)
[go practical basis] how to install and use gin

Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain

Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度

Introduction to the basic concept of queue and typical application examples

Linux安装Oracle Database 19c

Programmers with ten years of development experience tell you, what core competitiveness do you lack?
随机推荐
Image transformation, transpose
Connect function and disconnect function of QT
Don't spend money, spend an hour to build your own blog website
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
NPOI 导出Word 字号对应
C nail development: obtain all employee address books and send work notices
[go practical basis] how to bind and use URL parameters in gin
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
gocv边界填充
Minecraft install resource pack
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
随笔:RGB图像颜色分离(附代码)
WSL installation, beautification, network agent and remote development
C# 高德地图 根据经纬度获取地址
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
MYSQL安装出现问题(The service already exists)
C#钉钉开发:取得所有员工通讯录和发送工作通知
【Go实战基础】如何安装和使用 gin
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
