yjp 1 سال پیش
والد
کامیت
b6b8abe942
3فایلهای تغییر یافته به همراه51 افزوده شده و 10 حذف شده
  1. 32 0
      framework/core/api/api.go
  2. 9 0
      framework/core/api/options.go
  3. 10 10
      framework/gateway/builder_request.go

+ 32 - 0
framework/core/api/api.go

@@ -2,10 +2,15 @@ package api
 
 import (
 	"context"
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
 	"git.sxidc.com/go-tools/utils/strutils"
 	"github.com/gin-gonic/gin"
 	"github.com/pkg/errors"
 	"net/http"
+	"runtime/debug"
+	"slices"
+	"strconv"
+	"time"
 )
 
 type Api struct {
@@ -34,6 +39,33 @@ func New(opts ...Option) *Api {
 		Handler: engine,
 	}
 
+	engine.Use(
+		// 日志中间件
+		func(c *gin.Context) {
+			start := time.Now()
+			c.Next()
+
+			path := c.FullPath()
+			if slices.Contains(options.skipPaths, path) {
+				return
+			}
+
+			end := time.Now()
+			logger.GetInstance().Info(" %d | %s | %s		 \"%s\"",
+				c.Writer.Status(),
+				strconv.Itoa(int(end.Sub(start).Milliseconds()))+"ms",
+				c.Request.Method,
+				path,
+			)
+		},
+		// recover
+		func(c *gin.Context) {
+			err := recover()
+			if err != nil {
+				logger.GetInstance().Error("%s", debug.Stack())
+			}
+		})
+
 	api := &Api{
 		options:    *options,
 		server:     server,

+ 9 - 0
framework/core/api/options.go

@@ -6,6 +6,9 @@ type Options struct {
 
 	// 端口
 	port string
+
+	// 日志跳过的打印路径
+	skipPaths []string
 }
 
 func (options Options) GetPort() string {
@@ -29,3 +32,9 @@ func WithPort(port string) Option {
 		options.port = port
 	}
 }
+
+func WithSkipPaths(skipPaths []string) Option {
+	return func(options *Options) {
+		options.skipPaths = skipPaths
+	}
+}

+ 10 - 10
framework/gateway/builder_request.go

@@ -85,7 +85,7 @@ func (req *PostRequest) prepare(c *api.Context, historyRequests []BuilderRequest
 	}
 
 	// 赋值默认body
-	if req.Body == nil {
+	if preparedRequest.Body == nil {
 		cacheBody, err := c.GetBytesBody()
 		if err != nil {
 			return nil, err
@@ -178,13 +178,13 @@ func (req *DeleteRequest) prepare(c *api.Context, historyRequests []BuilderReque
 	}
 
 	// 赋值默认查询参数
-	if req.QueryParams == nil || len(req.QueryParams) == 0 {
-		req.QueryParams = c.GetQueryParams().Map()
+	if preparedRequest.QueryParams == nil || len(preparedRequest.QueryParams) == 0 {
+		preparedRequest.QueryParams = c.GetQueryParams().Map()
 	}
 
 	// 赋值默认路径参数
-	if req.PathParams == nil || len(req.PathParams) == 0 {
-		req.PathParams = c.GetPathParams().Map()
+	if preparedRequest.PathParams == nil || len(preparedRequest.PathParams) == 0 {
+		preparedRequest.PathParams = c.GetPathParams().Map()
 	}
 
 	// 赋值默认headers
@@ -275,7 +275,7 @@ func (req *PutRequest) prepare(c *api.Context, historyRequests []BuilderRequest,
 	}
 
 	// 赋值默认body
-	if req.Body == nil {
+	if preparedRequest.Body == nil {
 		cacheBody, err := c.GetBytesBody()
 		if err != nil {
 			return nil, err
@@ -368,13 +368,13 @@ func (req *GetRequest) prepare(c *api.Context, historyRequests []BuilderRequest,
 	}
 
 	// 赋值默认查询参数
-	if req.QueryParams == nil || len(req.QueryParams) == 0 {
-		req.QueryParams = c.GetQueryParams().Map()
+	if preparedRequest.QueryParams == nil || len(preparedRequest.QueryParams) == 0 {
+		preparedRequest.QueryParams = c.GetQueryParams().Map()
 	}
 
 	// 赋值默认路径参数
-	if req.PathParams == nil || len(req.PathParams) == 0 {
-		req.PathParams = c.GetPathParams().Map()
+	if preparedRequest.PathParams == nil || len(preparedRequest.PathParams) == 0 {
+		preparedRequest.PathParams = c.GetPathParams().Map()
 	}
 
 	// 赋值默认headers