forked from Treblle/treblle-go-gin
/
response.go
76 lines (63 loc) · 1.57 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package treblle
import (
"bytes"
"encoding/json"
"time"
"github.com/gin-gonic/gin"
)
type ResponseInfo struct {
Headers json.RawMessage `json:"headers"`
Code int `json:"code"`
Size int `json:"size"`
LoadTime float64 `json:"load_time"`
Body json.RawMessage `json:"body"`
Errors []ErrorInfo `json:"errors"`
}
type ErrorInfo struct {
Source string `json:"source"`
Type string `json:"type"`
Message string `json:"message"`
File string `json:"file"`
Line int `json:"line"`
}
type BodyLogWriter struct {
gin.ResponseWriter
Body *bytes.Buffer
}
// Extract information from Gin body and context
func getGinResponseInfo(blw *BodyLogWriter, c *gin.Context, startTime time.Time) ResponseInfo {
defer dontPanic()
responseBytes := blw.Body.Bytes()
errInfo := ErrorInfo{}
var body map[string]interface{}
err := json.Unmarshal(responseBytes, &body)
if err != nil {
errInfo.Message = err.Error()
}
headers := make(map[string]string)
for k := range blw.Header() {
headers[k] = blw.Header().Get(k)
}
// headers to Json
headersJson, err := json.Marshal(headers)
if err != nil {
errInfo.Message = err.Error()
}
// body to Json
bodyJson, err := json.Marshal(body)
if err != nil {
errInfo.Message = err.Error()
}
r := ResponseInfo{
Headers: headersJson,
Code: c.Writer.Status(),
Size: len(responseBytes),
LoadTime: float64(time.Since(startTime).Microseconds()),
Body: bodyJson,
Errors: make([]ErrorInfo, 0),
}
if err != nil {
r.Errors = append(r.Errors, errInfo)
}
return r
}