()
| 33 | ) |
| 34 | |
| 35 | func OperationLog() gin.HandlerFunc { |
| 36 | return func(c *gin.Context) { |
| 37 | c.Request.Header.Del(headerNeedOperationResolve) |
| 38 | |
| 39 | if strings.Contains(c.Request.URL.Path, "search") || c.Request.Method == http.MethodGet { |
| 40 | c.Next() |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | source := loadLogInfo(c.Request.URL.Path) |
| 45 | pathItem := strings.TrimPrefix(c.Request.URL.Path, "/api/v2") |
| 46 | pathItem = strings.TrimPrefix(pathItem, "/api/v2/core") |
| 47 | currentNodeItem := c.Request.Header.Get("CurrentNode") |
| 48 | currentNode, _ := url.QueryUnescape(currentNodeItem) |
| 49 | record := &model.OperationLog{ |
| 50 | Source: source, |
| 51 | Node: currentNode, |
| 52 | IP: c.ClientIP(), |
| 53 | Method: strings.ToLower(c.Request.Method), |
| 54 | Path: pathItem, |
| 55 | UserAgent: c.Request.UserAgent(), |
| 56 | } |
| 57 | swagger := make(map[string]operationJson) |
| 58 | if err := json.Unmarshal(docs.XLogJson, &swagger); err != nil { |
| 59 | c.Next() |
| 60 | return |
| 61 | } |
| 62 | operationDic, hasPath := swagger[record.Path] |
| 63 | if !hasPath { |
| 64 | if fullPath := normalizeOperationPath(c.FullPath()); fullPath != "" { |
| 65 | operationDic, hasPath = swagger[fullPath] |
| 66 | } |
| 67 | } |
| 68 | if !hasPath { |
| 69 | c.Next() |
| 70 | return |
| 71 | } |
| 72 | if len(operationDic.FormatZH) == 0 { |
| 73 | c.Next() |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | formatMap := make(map[string]interface{}) |
| 78 | if len(operationDic.BodyKeys) != 0 { |
| 79 | body, err := io.ReadAll(c.Request.Body) |
| 80 | if err == nil { |
| 81 | c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) |
| 82 | } |
| 83 | bodyMap := make(map[string]interface{}) |
| 84 | if strings.Contains(c.Request.Header.Get("Content-Type"), "multipart/form-data") { |
| 85 | bodyMap, _ = parseMultipart(body, c.Request.Header.Get("Content-Type")) |
| 86 | } else { |
| 87 | decoder := json.NewDecoder(bytes.NewReader(body)) |
| 88 | decoder.UseNumber() |
| 89 | _ = decoder.Decode(&bodyMap) |
| 90 | } |
| 91 | for _, key := range operationDic.BodyKeys { |
| 92 | if _, ok := bodyMap[key]; ok { |
nothing calls this directly
no test coverage detected