/
types.go
122 lines (102 loc) · 2.41 KB
/
types.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Originally from https://gl.oddhunters.com/pub/fiberzap
// Copyright (apparently) by Ozgur Boru <boruozgur@yandex.com.tr>
// and "mert" (https://gl.oddhunters.com/mert)
package fiberzap
import (
"bytes"
"encoding/json"
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
"go.uber.org/zap/zapcore"
"strings"
)
func getAllowedHeaders() map[string]bool {
return map[string]bool{
"User-Agent": true,
"X-Mobile": true,
}
}
type resp struct {
code int
_type string
}
func Resp(r *fasthttp.Response) *resp {
return &resp{
code: r.StatusCode(),
_type: bytes.NewBuffer(r.Header.ContentType()).String(),
}
}
func (r *resp) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("type", r._type)
enc.AddInt("code", r.code)
return nil
}
type req struct {
body string
fullPath string
user string
ip string
method string
route string
headers *headerbag
}
func Req(c *fiber.Ctx) *req {
reqq := c.Request()
var body []byte
buffer := new(bytes.Buffer)
err := json.Compact(buffer, reqq.Body())
if err != nil {
body = reqq.Body()
} else {
body = buffer.Bytes()
}
headers := &headerbag{
vals: make(map[string]string),
}
allowedHeaders := getAllowedHeaders()
reqq.Header.VisitAll(func(key, val []byte) {
k := bytes.NewBuffer(key).String()
if _, exist := allowedHeaders[k]; exist {
headers.vals[strings.ToLower(k)] = bytes.NewBuffer(val).String()
}
})
var userEmail string
if u := c.Locals("userEmail"); u != nil {
userEmail = u.(string)
}
return &req{
body: bytes.NewBuffer(body).String(),
fullPath: bytes.NewBuffer(reqq.RequestURI()).String(),
headers: headers,
ip: c.IP(),
method: c.Method(),
route: c.Route().Path,
user: userEmail,
}
}
func (r *req) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("fullPath", r.fullPath)
enc.AddString("ip", r.ip)
enc.AddString("method", r.method)
enc.AddString("route", r.route)
if r.body != "" {
enc.AddString("body", r.body)
}
if r.user != "" {
enc.AddString("user", r.user)
}
err := enc.AddObject("headers", r.headers)
if err != nil {
return err
}
return nil
}
type headerbag struct {
vals map[string]string
}
func (h *headerbag) MarshalLogObject(enc zapcore.ObjectEncoder) error {
for k, v := range h.vals {
enc.AddString(k, v)
}
return nil
}