当前位置:网站首页>[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 !
边栏推荐
- 统计字符串中各类字符的个数
- Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
- Select sort and insert sort
- Kubernetes deploys Loki logging system
- Linux安装Oracle Database 19c
- Oracle related statistics
- C call system sound beep~
- commands out of sync. did you run multiple statements at once
- win10使用docker拉取redis镜像报错read-only file system: unknown
- oracle修改数据库字符集
猜你喜欢

Cloud computing in my eyes - PAAS (platform as a service)

Hengyuan cloud_ Can aiphacode replace programmers?
![[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)](/img/7f/2cd789339237b7a881bfed7b7545a9.jpg)
[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)

Linux二进制安装Oracle Database 19c

【Go实战基础】gin 如何验证请求参数

Talk about the secret of high performance of message queue -- zero copy technology

Openshift container platform community okd 4.10.0 deployment

Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc

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

Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
随机推荐
Redis zadd导致的一次线上问题排查和处理
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
How to realize asynchronous programming in a synchronous way?
C language - Blue Bridge Cup - 7 segment code
Linux binary installation Oracle database 19C
「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
Multi version concurrency control mvcc of MySQL
使用递归函数求解字符串的逆置问题
Oracle修改表空间名称以及数据文件
C call system sound beep~
Solution and analysis of Hanoi Tower problem
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
「Redis源码系列」关于源码阅读的学习与思考
小米电视不能访问电脑共享文件的解决方案
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
First week of JS study
聊聊消息队列高性能的秘密——零拷贝技术
Function ‘ngram‘ is not defined
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
