当前位置:网站首页>【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 路由和回调
菜鸟实战,持续学习!
边栏推荐
- C nail development: obtain all employee address books and send work notices
- Getting started with k8s: building MySQL with Helm
- Loadbalancer dynamically refreshes Nacos server
- Minecraft air Island service
- 1、 QT's core class QObject
- Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
- 使用递归函数求解字符串的逆置问题
- Qt QTimer类
- libusb的使用
- Webflux responsive programming
猜你喜欢

OpenFeign 簡單使用

Realization of basic function of sequence table

C nail development: obtain all employee address books and send work notices

C#钉钉开发:取得所有员工通讯录和发送工作通知

Avoid breaking changes caused by modifying constructor input parameters

Minecraft air Island service

Luogu greedy part of the backpack line segment covers the queue to receive water

Redis安装部署(Windows/Linux)

Function ‘ngram‘ is not defined

win10使用docker拉取redis镜像报错read-only file system: unknown
随机推荐
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Linux binary installation Oracle database 19C
win10使用docker拉取redis镜像报错read-only file system: unknown
C Gaode map obtains the address according to longitude and latitude
Leetcode sword finger offer brush questions - day 23
MYSQL安装出现问题(The service already exists)
2022/2/13 summary
C#钉钉开发:取得所有员工通讯录和发送工作通知
C# 高德地图 根据经纬度获取地址
Qt——如何在QWidget中设置阴影效果
C# 将网页保存为图片(利用WebBrowser)
Oracle related statistics
Installing Oracle database 19C for Linux
Judge whether it is Sudoku
libusb的使用
图像变换,转置
Linux二进制安装Oracle Database 19c
Minecraft群組服開服
队列的基本概念介绍以及典型应用示例
Data asset management function
