当前位置:网站首页>Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
2022-07-03 13:47:00 【Lao Liu, you are so awesome】
One , Libraries required for installation
1,gin In the framework of github The address of :
https://github.com/gin-gonic/gin
2, Install... From the command line :
[email protected]:/data/go/ginhello# go get -u github.com/gin-gonic/[email protected]
explain : Liu Hongdi's go The forest is a focus golang The blog of ,
Address :https://blog.csdn.net/weixin_43881017
explain : author : Liu Hongdi mailbox : [email protected]
Two , Information about the demonstration project
1, Project address :
https://github.com/liuhongdi/digv26
2, Functional specifications : Demonstrate using multiple middleware( middleware ) when ,middleware The execution order of the code in
3, Project structure : Pictured :
3、 ... and ,go Code instructions
1,middleware/middle.go
package middleware
import (
"fmt"
"github.com/gin-gonic/gin"
"time"
)
// first middleware
func MiddlewareOne() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("before middlewareOne: "+time.Now().String())
c.Next()
fmt.Println("after middlewareOne: "+time.Now().String())
return
}
}
// the second middleware
func MiddlewareTwo() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("before middlewareTwo: "+time.Now().String())
c.Next()
fmt.Println("after middlewareTwo: "+time.Now().String())
return
}
}
// Third middleware
func MiddlewareThree() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("before MiddlewareThree: "+time.Now().String())
c.Next()
fmt.Println("after MiddlewareThree: "+time.Now().String())
return
}
}
2,router/router.go
package router
import (
"github.com/gin-gonic/gin"
"github.com/liuhongdi/digv26/controller"
"github.com/liuhongdi/digv26/global"
"github.com/liuhongdi/digv26/middleware"
"log"
"runtime/debug"
)
func Router() *gin.Engine {
router := gin.Default()
// Handling exceptions
router.NoRoute(HandleNotFound)
router.NoMethod(HandleNotFound)
//use middleware
router.Use(middleware.MiddlewareTwo())
router.Use(middleware.MiddlewareOne())
router.Use(middleware.MiddlewareThree())
router.Use(Recover)
// Path mapping :index
indexc:=controller.NewIndexController()
router.GET("/index/index", indexc.Index);
return router
}
func HandleNotFound(c *gin.Context) {
global.NewResult(c).Error(404," Resource not found ")
return
}
func Recover(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
// Print error stack information
log.Printf("panic: %v\n", r)
debug.PrintStack()
global.NewResult(c).Error(500," Server internal error ")
}
}()
// Finished loading defer recover, Continue with subsequent interface calls
c.Next()
}
3,controller/indexController.go
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/liuhongdi/digv26/global"
"time"
)
type IndexController struct{}
func NewIndexController() IndexController {
return IndexController{}
}
// Return a successful prompt
func (g *IndexController) Index(c *gin.Context) {
fmt.Println("controller:index: "+time.Now().String())
result := global.NewResult(c)
result.Success("success");
return
}
Four , The test results
visit :
http://127.0.0.1:8080/index/index
return :
Look at the output of the console :
before middlewareTwo: 2021-02-03 11:52:29.84260954 +0800 CST m=+3.560203369
before middlewareOne: 2021-02-03 11:52:29.842749172 +0800 CST m=+3.560342999
before MiddlewareThree: 2021-02-03 11:52:29.842758214 +0800 CST m=+3.560352049
controller:index: 2021-02-03 11:52:29.842761431 +0800 CST m=+3.560355255
after MiddlewareThree: 2021-02-03 11:52:29.842819585 +0800 CST m=+3.560413419
after middlewareOne: 2021-02-03 11:52:29.842825746 +0800 CST m=+3.560419571
after middlewareTwo: 2021-02-03 11:52:29.842828523 +0800 CST m=+3.560422350
You can see :
There are many. middleware when
about next Previous code , Is in accordance with the use The order of takes effect ,
and next Later code , It takes effect in the reverse order
5、 ... and , View the version of the library :
module github.com/liuhongdi/digv26
go 1.15
require (
github.com/gin-gonic/gin v1.6.3
)
边栏推荐
- Halcon combined with C # to detect surface defects -- Halcon routine autobahn
- Internet of things completion -- (stm32f407 connects to cloud platform detection data)
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- 使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
- Task6: using transformer for emotion analysis
- untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- Libuv Library - Design Overview (Chinese version)
- Software testing is so hard to find, only outsourcing offers, should I go?
- 全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
猜你喜欢
太阳底下无新事,元宇宙能否更上层楼?
Brief analysis of tensorboard visual processing cases
8 Queen question
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
Libuv库 - 设计概述(中文版)
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
MySQL functions and related cases and exercises
Flutter dynamic | fair 2.5.0 new version features
SQL Injection (POST/Select)
Several common optimization methods matlab principle and depth analysis
随机推荐
Red hat satellite 6: better management of servers and clouds
[sort] bucket sort
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
php 迷宫游戏
Red Hat Satellite 6:更好地管理服务器和云
Task6: using transformer for emotion analysis
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
栈应用(平衡符)
The solution of Chinese font garbled code in keil5
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
[556. Next larger element III]
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
Sequence table (implemented in C language)
太阳底下无新事,元宇宙能否更上层楼?
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
Logseq 评测:优点、缺点、评价、学习教程
DQL basic query