当前位置:网站首页>【Go实战基础】gin 如何自定义和使用一个中间件
【Go实战基础】gin 如何自定义和使用一个中间件
2022-07-02 06:33:00 【菜鸟实战】
目录
一、简介
实战场景: gin 如何自定义和使用一个中间件
二、知识点
gin 路由
字符串数组
数组循环
自定义中间件
http 状态码
三、菜鸟实战
马上安排!
1、创建 go文件
/*
* @Author: 菜鸟实战
* @Description: gin 如何自定义和使用一个中间件
*/
// 知识点:
// # gin 路由
// # 字符串数组
// # 数组循环
// # 自定义中间件
// # http 状态码
package main
// 导入包
import (
"fmt"
"github.com/gin-gonic/gin"
"runtime"
)
// IP 授权中间件
func IPAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// 白名单
whiteIpList := []string{
"127.0.0.1",
}
flag := false
// 获取当前访问 ip, 与白名单 ip做比较
clientIp := c.ClientIP()
fmt.Printf("当前 client ip 为: %s \n", clientIp)
for _, whiteIp := range whiteIpList {
// 验证通过
if clientIp == whiteIp {
flag = true
break
}
}
// 验证未通过
if flag == false {
c.String(401, "client ip % is not in the white ip list", clientIp)
// 需要 abort
c.Abort()
}
}
}
// 主函数
func main() {
// 使用内置函数打印
println("Hello", "菜鸟实战")
println("实战场景: ", "gin 如何自定义和使用一个中间件")
// 初始化
r := gin.Default()
// 使用中间件
r.Use(IPAuthMiddleware())
// 添加 get 路由和回调
r.GET("/g", func(c *gin.Context) {
// 返回的 code 和 字符串返回
c.String(200, "这是一个使用了中间件的方法 \n")
})
// 使用包函数打印
fmt.Printf("版本: %s \n", runtime.Version())
// 启动框架程序, 默认 8080 端口
r.Run()
}
// curl 验证方式
// bind get
// curl -X GET "http://127.0.0.1:8080/g"
2、运行结果
Hello 菜鸟实战
实战场景: gin 如何自定义和使用一个中间件
[GIN-debug] Listening and serving HTTP on :8080
添加 get 路由和回调
菜鸟实战,持续学习!
边栏推荐
- 图像变换,转置
- [blackmail virus data recovery] suffix Crylock blackmail virus
- QT -- how to set shadow effect in QWidget
- Openfeign facile à utiliser
- Kubernetes deploys Loki logging system
- Qt的connect函数和disconnect函数
- PCL calculates the intersection of three mutually nonparallel planes
- Openfeign is easy to use
- Googlenet network explanation and model building
- Sqli labs level 8 (Boolean blind note)
猜你喜欢

Minecraft air Island service

Googlenet network explanation and model building

Avoid breaking changes caused by modifying constructor input parameters

Qt——如何在QWidget中设置阴影效果

Openshift deployment application

WSL安装、美化、网络代理和远程开发

Kubernetes deploys Loki logging system
![[blackmail virus data recovery] suffix Rook3 blackmail virus](/img/46/debc848d17767d021f3f41924cccfe.jpg)
[blackmail virus data recovery] suffix Rook3 blackmail virus

What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?

OpenFeign 簡單使用
随机推荐
gocv拆分颜色通道
Finishing the interview essentials of secsha system!!!
OpenFeign 簡單使用
Essay: RGB image color separation (with code)
commands out of sync. did you run multiple statements at once
Judge whether it is Sudoku
Linux binary installation Oracle database 19C
Loadbalancer dynamically refreshes Nacos server
Gocv image reading and display
Synchronize files using unison
Web security -- core defense mechanism
判断是否是数独
Application of kotlin - higher order function
Function ‘ngram‘ is not defined
HackTheBox-Gunship
gocv opencv exit status 3221225785
Tensorflow2 keras classification model
Leetcode sword finger offer brush questions - day 22
Redis安装部署(Windows/Linux)
KubeSphere 虚拟化 KSV 安装体验
