From 8bcd1a42948b19043c927f88f7502f2516d8065a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Tue, 9 Mar 2021 18:08:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0GIN=20-=20InitRequest?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/middleware/init_request.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 gin/middleware/init_request.go diff --git a/gin/middleware/init_request.go b/gin/middleware/init_request.go new file mode 100644 index 0000000..b12aca8 --- /dev/null +++ b/gin/middleware/init_request.go @@ -0,0 +1,30 @@ +// Package middleware... +// +// Description : middleware... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-03-09 5:52 下午 +package middleware + +import ( + "time" + + "github.com/gin-gonic/gin" + commonUtil "github.com/go-developer/gopkg/util" +) + +// InitRequest 初始化请求信息,统一设置请求时间/请求ID等信息 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:53 下午 2021/3/9 +func InitRequest() func(ctx *gin.Context) { + return func(ctx *gin.Context) { + // 设置请求开始时间 + ctx.Set("start_time", time.Now().Unix()) + // 设置请求trace_id + ctx.Set("trace_id", time.Now().Format("20060102150405")+"+"+commonUtil.GetHostIP()+"-"+commonUtil.Md5(commonUtil.GenRandomString("", 16))) + ctx.Next() + } +}