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"