refactor: 移动目录
This commit is contained in:
parent
5f831c7005
commit
9ad207f6c9
@ -5,13 +5,13 @@
|
|||||||
// Distributed under terms of the MIT license.
|
// Distributed under terms of the MIT license.
|
||||||
//
|
//
|
||||||
|
|
||||||
package middleware
|
package cors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"git.hexq.cn/tiglog/golib/helper"
|
"git.hexq.cn/tiglog/golib/helper"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCors(origins []string) gin.HandlerFunc {
|
func NewCors(origins []string) gin.HandlerFunc {
|
@ -5,7 +5,7 @@
|
|||||||
// Distributed under terms of the MIT license.
|
// Distributed under terms of the MIT license.
|
||||||
//
|
//
|
||||||
|
|
||||||
package middleware_test
|
package cors_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -13,15 +13,15 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"git.hexq.cn/tiglog/golib/gtest"
|
"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 {
|
func newTestRouter(origins []string) *gin.Engine {
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
router.Use(middleware.NewCors(origins))
|
router.Use(cors.NewCors(origins))
|
||||||
router.GET("/", func(c *gin.Context) {
|
router.GET("/", func(c *gin.Context) {
|
||||||
c.String(http.StatusOK, "get")
|
c.String(http.StatusOK, "get")
|
||||||
})
|
})
|
@ -5,7 +5,7 @@
|
|||||||
// Distributed under terms of the MIT license.
|
// Distributed under terms of the MIT license.
|
||||||
//
|
//
|
||||||
|
|
||||||
package middleware
|
package ginlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -32,8 +32,7 @@ func GinLogger(logfile string) gin.HandlerFunc {
|
|||||||
if latency > time.Minute {
|
if latency > time.Minute {
|
||||||
latency = latency.Truncate(time.Second)
|
latency = latency.Truncate(time.Second)
|
||||||
}
|
}
|
||||||
|
fields := []logger.Field{
|
||||||
log.Info("GIN request",
|
|
||||||
logger.String("start", start.Format(time.RFC3339)),
|
logger.String("start", start.Format(time.RFC3339)),
|
||||||
logger.Int("status", c.Writer.Status()),
|
logger.Int("status", c.Writer.Status()),
|
||||||
logger.String("latency", fmt.Sprintf("%s", latency)),
|
logger.String("latency", fmt.Sprintf("%s", latency)),
|
||||||
@ -43,6 +42,11 @@ func GinLogger(logfile string) gin.HandlerFunc {
|
|||||||
logger.String("clientIP", c.ClientIP()),
|
logger.String("clientIP", c.ClientIP()),
|
||||||
logger.String("userAgent", c.Request.UserAgent()),
|
logger.String("userAgent", c.Request.UserAgent()),
|
||||||
logger.String("error", c.Errors.ByType(gin.ErrorTypePrivate).String()),
|
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...)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,7 +5,7 @@
|
|||||||
// Distributed under terms of the MIT license.
|
// Distributed under terms of the MIT license.
|
||||||
//
|
//
|
||||||
|
|
||||||
package middleware
|
package ginlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
Loading…
Reference in New Issue
Block a user