-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler_helper.go
96 lines (86 loc) · 1.87 KB
/
handler_helper.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package http
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
)
// TraceHandler
// If you want start trace, use this on your root router
func TraceHandler(c *Context) {
c.Request.Header.Set("Kelp-Traceid", randMd5())
c.Next()
}
func RecoveryHandler(c *Context) {
defer func() {
if err := recover(); err != nil {
log.Log("ERROR", "[panic]", err)
c.DieWithHttpStatus(500)
}
}()
c.Next()
}
func LogHandler(c *Context) {
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
ips := c.Request.Header.Get("X-Forwarded-For")
ip := ""
if ips != "" {
ip = strings.Split(ips, ",")[0]
}
if ip == "" {
ip = c.Request.Header.Get("X-Real-Ip")
}
if ip == "" {
ip = c.Request.RemoteAddr
}
c.Next()
traceId := c.Request.Header.Get("Kelp-Traceid")
uuid := c.Request.Header.Get("uuid")
end := time.Now()
latency := end.Sub(start)
method := c.Request.Method
resp := string(c.Response)
if len(resp) > 500 {
resp = fmt.Sprintf("response is too large (with %d bytes, head is %s)", len(resp), resp[0:100]+"...")
}
req := string(c.Body())
if raw != "" {
path = path + "?" + raw
}
log.Log(
"REQ",
ip, // remote ip
end.Format("2006/01/02 15:04:05"),
latency.Nanoseconds()/int64(time.Millisecond),
str(method),
str(path),
str(traceId), // trace id
str(uuid), // uuid
`"""`+str(req)+`"""`,
`"""`+str(resp)+`"""`,
)
}
func str(v string) string {
if v == "" {
return "-"
}
return v
}
func randMd5() string {
timestamp := []byte(strconv.FormatInt(time.Now().Unix(), 10))
prefix := []byte(strconv.Itoa(rand.Intn(10000)))
surfix := []byte(strconv.Itoa(rand.Intn(10000)))
seed := bytes.Join([][]byte{prefix, timestamp, surfix}, []byte(""))
h := md5.New()
h.Write(seed)
data := h.Sum(nil)
dst := make([]byte, hex.EncodedLen(len(data)))
hex.Encode(dst, data)
return string(dst)
}