快速开发-基于gin的中间件web项目开发

一、概述

在 Gin 框架中,中间件(Middleware)是一种在处理 HTTP 请求之前或之后执行的函数。使用中间件可以方便地实现诸如身份验证(Auth)、日志记录、请求限流等功能。

二、编写中间件

复制代码
// 中间件
func StartCost1(c *gin.Context) {
	log.Info("......start1......")
	start := time.Now()
	c.Next() //调用后续的处理函数
	// c.Abort() //阻止调用后续的处理函数
	cost := time.Since(start)
	log.Info("cost1:", cost)
	log.Info("......end1......")
}

func StartCost2(c *gin.Context) {
	log.Info("......start2......")

	log.Info("......end2......")
}

func authMiddleware(doCheck bool) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		if doCheck {
			//检查用户信息
			ctx.Set("token", "20240814")
			// TODO
			log.Info("检查用户登录信息")
			ctx.Next()
		} else {
			//不检查用户信息
			log.Error("不检查用户信息即可处理")
			ctx.Next()
		}
	}
}

三、使用中间件

python 复制代码
func main() {

	r := gin.Default()

	r.Use(StartCost1, StartCost2, authMiddleware(true))
	r.LoadHTMLFiles("./index.html", "./home.html")

	r.GET("/web", func(c *gin.Context) {
		name := c.Query("query")
		c.JSON(http.StatusOK, gin.H{
			"name": name,
		})
	})

	r.GET("/login", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "index.html", nil)
	})
	r.GET("/home", func(ctx *gin.Context) {
		ctx.Request.URL.Path = "/b"
		r.HandleContext(ctx)
	})

	r.GET("/b", func(ctx *gin.Context) {
		log.Info("request /b GetLocalIP: " + GetLocalIP())
		ctx.Redirect(http.StatusMovedPermanently, GetLocalIP()+":9090/login")
	})

	r.POST("/login", func(ctx *gin.Context) {
		log.Info("request /login GetLocalIP: " + GetLocalIP())
		username := ctx.PostForm("username")
		password := ctx.PostForm("password")
		//从请求中获取携带的参数
		f, err := ctx.FormFile("file")
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"error": err,
			})
		} else {
			filePath := path.Join("./", f.Filename)
			ctx.SaveUploadedFile(f, filePath)
			ctx.HTML(http.StatusOK, "home.html", gin.H{
				"Name":     username,
				"Password": password,
				"Token":    ctx.GetString("token"),
			})
		}

	})

	r.Run(":9090")

}

四、后端完整代码

python 复制代码
package main

import (
	"net"
	"net/http"
	"path"
	"time"

	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

func GetLocalIP() string {

	ip := "127.0.0.1"

	addrs, err := net.InterfaceAddrs()

	if err != nil {
		return ip
	}
	for _, a := range addrs {
		if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				ip = ipnet.IP.String()
				break
			}
		}
	}
	return ip

}

// 中间件
func StartCost1(c *gin.Context) {
	log.Info("......start1......")
	start := time.Now()
	c.Next() //调用后续的处理函数
	// c.Abort() //阻止调用后续的处理函数
	cost := time.Since(start)
	log.Info("cost1:", cost)
	log.Info("......end1......")
}

func StartCost2(c *gin.Context) {
	log.Info("......start2......")

	log.Info("......end2......")
}

func authMiddleware(doCheck bool) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		if doCheck {
			//检查用户信息
			ctx.Set("token", "20240814")
			// TODO
			log.Info("检查用户登录信息")
			ctx.Next()
		} else {
			//不检查用户信息
			log.Error("不检查用户信息即可处理")
			ctx.Next()
		}
	}
}

func main() {

	r := gin.Default()

	r.Use(StartCost1, StartCost2, authMiddleware(true))
	r.LoadHTMLFiles("./index.html", "./home.html")

	r.GET("/web", func(c *gin.Context) {
		name := c.Query("query")
		c.JSON(http.StatusOK, gin.H{
			"name": name,
		})
	})

	r.GET("/login", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "index.html", nil)
	})
	r.GET("/home", func(ctx *gin.Context) {
		ctx.Request.URL.Path = "/b"
		r.HandleContext(ctx)
	})

	r.GET("/b", func(ctx *gin.Context) {
		log.Info("request /b GetLocalIP: " + GetLocalIP())
		ctx.Redirect(http.StatusMovedPermanently, GetLocalIP()+":9090/login")
	})

	r.POST("/login", func(ctx *gin.Context) {
		log.Info("request /login GetLocalIP: " + GetLocalIP())
		username := ctx.PostForm("username")
		password := ctx.PostForm("password")
		//从请求中获取携带的参数
		f, err := ctx.FormFile("file")
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"error": err,
			})
		} else {
			filePath := path.Join("./", f.Filename)
			ctx.SaveUploadedFile(f, filePath)
			ctx.HTML(http.StatusOK, "home.html", gin.H{
				"Name":     username,
				"Password": password,
				"Token":    ctx.GetString("token"),
			})
		}

	})

	r.Run(":9090")

}

五、前端页面

1、index.html

python 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Form</title>
</head>

<body>
    <form action="/login" method="post" enctype="multipart/form-data">

        <div>
            <label for="username">用户名:</label>
            <input type="text" name="username" id="username" />
        </div>
        <div>
            <label for="password">密 码:</label>
            <input type="text" name="password" id="password" />
        </div>
        <div>
            <label for="file">上传文件:</label>
            <input type="file" name="file" id="file" />
        </div>
        <div>
            <input type="submit" value="提交">
        </div>

    </form>

</body>

</html>

2、home.html

python 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户信息</title>
</head>

<body>
    <h1> 您好,{{.Name}}</h1>
    <span>密码:{{.Password}}</span>
    <span>Token:{{.Token}}</span>
</body>

</html>

六、运行代码

go run main.go

七、展示

相关推荐
阿昌喜欢吃黄桃5 天前
RocketMq事务消息原理
java·中间件·消息队列·rocketmq·mq
半夜修仙6 天前
延迟队列的介绍及常见问题
java·数据库·中间件·rabbitmq
手握风云-6 天前
一条消息的旅程:RabbitMQ 学习与实践(一)
中间件·rabbitmq
RH2312117 天前
2026.6.8Linux
java·数据库·中间件
理人综艺好会8 天前
双Token机制在实际项目中的应用与实践
中间件·token
番茄去哪了8 天前
神领物流面试题(一)
java·大数据·中间件
念何架构之路8 天前
消息中间件
中间件
都说名字长不会被发现8 天前
Spring Boot Starter 中间件账号密码加密方案设计与实现
java·spring boot·后端·中间件
瀚高PG实验室9 天前
java中间件无法连接数据库
java·数据库·中间件·瀚高数据库
之歆9 天前
Day11_Express 深入解析:从中间件到项目实战
中间件·express