当前位置:网站首页>[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 !
边栏推荐
猜你喜欢

机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案

Installing Oracle database 19C RAC on Linux

Multi version concurrency control mvcc of MySQL

Minecraft install resource pack

Matplotlib剑客行——容纳百川的艺术家教程

十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?

win10使用docker拉取redis镜像报错read-only file system: unknown

Flink - use the streaming batch API to count the number of words

C language - Blue Bridge Cup - 7 segment code

ORA-12514问题解决方法
随机推荐
我服了,MySQL表500W行,居然有人不做分区?
Leetcode sword finger offer brush questions - day 22
Openshift container platform community okd 4.10.0 deployment
【Go实战基础】如何安装和使用 gin
CSDN Q & A_ Evaluation
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
Move a string of numbers backward in sequence
图像变换,转置
盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
Redis安装部署(Windows/Linux)
一、Qt的核心类QObject
QT -- how to set shadow effect in QWidget
Hengyuan cloud_ Can aiphacode replace programmers?
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
寻找链表中值域最小的节点并移到链表的最前面
commands out of sync. did you run multiple statements at once
C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
【Go实战基础】gin 如何验证请求参数
Matplotlib剑客行——容纳百川的艺术家教程
