From 9ad207f6c96658431f6a194f9415d7bcd4a5c827 Mon Sep 17 00:00:00 2001 From: tiglog Date: Tue, 17 Oct 2023 17:21:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E5=8A=A8=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {middleware => gweb/cors}/cors.go | 4 ++-- {middleware => gweb/cors}/cors_test.go | 8 ++++---- {middleware => gweb/ginlog}/access_log.go | 12 ++++++++---- {middleware => gweb/ginlog}/recover_log.go | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) rename {middleware => gweb/cors}/cors.go (97%) rename {middleware => gweb/cors}/cors_test.go (97%) rename {middleware => gweb/ginlog}/access_log.go (82%) rename {middleware => gweb/ginlog}/recover_log.go (99%) diff --git a/middleware/cors.go b/gweb/cors/cors.go similarity index 97% rename from middleware/cors.go rename to gweb/cors/cors.go index 6a219f5..ae89373 100644 --- a/middleware/cors.go +++ b/gweb/cors/cors.go @@ -5,13 +5,13 @@ // Distributed under terms of the MIT license. // -package middleware +package cors import ( "net/http" - "github.com/gin-gonic/gin" "git.hexq.cn/tiglog/golib/helper" + "github.com/gin-gonic/gin" ) func NewCors(origins []string) gin.HandlerFunc { diff --git a/middleware/cors_test.go b/gweb/cors/cors_test.go similarity index 97% rename from middleware/cors_test.go rename to gweb/cors/cors_test.go index b30a516..adcd5b1 100644 --- a/middleware/cors_test.go +++ b/gweb/cors/cors_test.go @@ -5,7 +5,7 @@ // Distributed under terms of the MIT license. // -package middleware_test +package cors_test import ( "context" @@ -13,15 +13,15 @@ import ( "net/http/httptest" "testing" - "github.com/gin-gonic/gin" "git.hexq.cn/tiglog/golib/gtest" - "git.hexq.cn/tiglog/golib/middleware" + "git.hexq.cn/tiglog/golib/gweb/cors" + "github.com/gin-gonic/gin" ) func newTestRouter(origins []string) *gin.Engine { gin.SetMode(gin.TestMode) router := gin.New() - router.Use(middleware.NewCors(origins)) + router.Use(cors.NewCors(origins)) router.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "get") }) diff --git a/middleware/access_log.go b/gweb/ginlog/access_log.go similarity index 82% rename from middleware/access_log.go rename to gweb/ginlog/access_log.go index 8d15b2c..4d5781c 100644 --- a/middleware/access_log.go +++ b/gweb/ginlog/access_log.go @@ -5,7 +5,7 @@ // Distributed under terms of the MIT license. // -package middleware +package ginlog import ( "fmt" @@ -32,8 +32,7 @@ func GinLogger(logfile string) gin.HandlerFunc { if latency > time.Minute { latency = latency.Truncate(time.Second) } - - log.Info("GIN request", + fields := []logger.Field{ logger.String("start", start.Format(time.RFC3339)), logger.Int("status", c.Writer.Status()), logger.String("latency", fmt.Sprintf("%s", latency)), @@ -43,6 +42,11 @@ func GinLogger(logfile string) gin.HandlerFunc { logger.String("clientIP", c.ClientIP()), logger.String("userAgent", c.Request.UserAgent()), logger.String("error", c.Errors.ByType(gin.ErrorTypePrivate).String()), - ) + } + if requestID := c.Writer.Header().Get("X-Request-Id"); requestID != "" { + fields = append(fields, logger.String("request_id", requestID)) + } + + log.Info("GIN request", fields...) } } diff --git a/middleware/recover_log.go b/gweb/ginlog/recover_log.go similarity index 99% rename from middleware/recover_log.go rename to gweb/ginlog/recover_log.go index 8b2d9de..7863ca0 100644 --- a/middleware/recover_log.go +++ b/gweb/ginlog/recover_log.go @@ -5,7 +5,7 @@ // Distributed under terms of the MIT license. // -package middleware +package ginlog import ( "errors"