This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fiberx.go
227 lines (183 loc) · 5.96 KB
/
fiberx.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package fiberx
import (
"os"
"strings"
"time"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
)
// ErrHandler is Dawn's error handler
var ErrHandler = func(c *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
res := Response{
Code: code,
Message: utils.StatusMessage(code),
}
if errs, ok := err.(validator.ValidationErrors); ok {
res.Code = fiber.StatusUnprocessableEntity
res.Message = ""
res.Data = removeTopStruct(errs.Translate(trans))
} else if e, ok := err.(*fiber.Error); ok {
res.Code = e.Code
res.Message = e.Message
}
return Resp(c, res.Code, res)
}
func removeTopStruct(fields map[string]string) map[string]string {
res := map[string]string{}
for field, msg := range fields {
stripStruct := field[strings.Index(field, ".")+1:]
res[stripStruct] = strings.TrimLeft(msg, stripStruct)
}
return res
}
// ValidateBody accepts a obj holds results from BodyParser
// and then do the validation by a validator
func ValidateBody(c *fiber.Ctx, obj interface{}) (err error) {
if err := c.BodyParser(obj); err != nil {
return err
}
return V.Struct(obj)
}
// ValidateQuery accepts a obj holds results from QueryParser
// and then do the validation by a validator
func ValidateQuery(c *fiber.Ctx, obj interface{}) (err error) {
if err := c.QueryParser(obj); err != nil {
return err
}
return V.Struct(obj)
}
// Response is a unified format for api results
type Response struct {
// Code is the status code by default, but also can be
// a custom code
Code int `json:"code,omitempty"`
// Message shows detail thing back to caller
Message string `json:"message,omitempty"`
// RequestID needs to be used with middleware
RequestID string `json:"request_id,omitempty"`
// Data accepts any thing as the response data
Data interface{} `json:"data,omitempty"`
}
// Resp returns the custom response
func Resp(c *fiber.Ctx, statusCode int, res Response) error {
if res.Code == 0 {
res.Code = statusCode
}
if id := c.Response().Header.Peek(fiber.HeaderXRequestID); len(id) > 0 && res.RequestID == "" {
res.RequestID = utils.GetString(id)
}
return c.Status(statusCode).JSON(res)
}
// Data returns data with status code OK by default
func Data(c *fiber.Ctx, data interface{}) error {
return Resp(c, fiber.StatusOK, Response{Data: data})
}
// Message wraps for RespOK with required message
func Message(c *fiber.Ctx, msg string) error {
return RespOK(c, msg)
}
// respCommon
func respCommon(c *fiber.Ctx, code int, msg ...string) error {
res := Response{
Message: utils.StatusMessage(code),
}
if len(msg) > 0 {
res.Message = msg[0]
}
return Resp(c, code, res)
}
// RespOK responses with status code 200 RFC 7231, 6.3.1
func RespOK(c *fiber.Ctx, msg ...string) error {
return respCommon(c, fiber.StatusOK, msg...)
}
// RespCreated responses with status code 201 RFC 7231, 6.3.2
func RespCreated(c *fiber.Ctx, msg ...string) error {
return respCommon(c, fiber.StatusCreated, msg...)
}
// RespAccepted responses with status code 202 RFC 7231, 6.3.3
func RespAccepted(c *fiber.Ctx, msg ...string) error {
return respCommon(c, fiber.StatusAccepted, msg...)
}
// RespNonAuthoritativeInformation responses with status code 203 RFC 7231, 6.3.4
func RespNonAuthoritativeInformation(c *fiber.Ctx, msg ...string) error {
return respCommon(c, fiber.StatusNonAuthoritativeInformation, msg...)
}
// RespNoContent responses with status code 204 RFC 7231, 6.3.5
func RespNoContent(c *fiber.Ctx, msg ...string) error {
return respCommon(c, fiber.StatusNoContent, msg...)
}
// RespResetContent responses with status code 205 RFC 7231, 6.3.6
func RespResetContent(c *fiber.Ctx, msg ...string) error {
return respCommon(c, fiber.StatusResetContent, msg...)
}
// RespPartialContent responses with status code 206 RFC 7233, 4.1
func RespPartialContent(c *fiber.Ctx, msg ...string) error {
return respCommon(c, fiber.StatusPartialContent, msg...)
}
// RespMultiStatus responses with status code 207 RFC 4918, 11.1
func RespMultiStatus(c *fiber.Ctx, msg ...string) error {
return respCommon(c, fiber.StatusMultiStatus, msg...)
}
// RespAlreadyReported responses with status code 208 RFC 5842, 7.1
func RespAlreadyReported(c *fiber.Ctx, msg ...string) error {
return respCommon(c, fiber.StatusAlreadyReported, msg...)
}
var pid = os.Getpid()
// Logger logs request and response info to os.Stdout
// or os.Stderr. The format is:
// time #pid[-request-id]: latency status clientIP method protocol://host_path[ error]
func Logger() fiber.Handler {
return func(ctx *fiber.Ctx) (err error) {
start := time.Now()
err = ctx.Next()
end := time.Now()
latency := end.Sub(start).Truncate(time.Microsecond)
bb := bytebufferpool.Get()
defer bytebufferpool.Put(bb)
// append time
bb.B = end.AppendFormat(bb.B, "2006/01/02 15:04:05.000")
// append pid
_, _ = bb.WriteString(" #")
bb.B = fasthttp.AppendUint(bb.B, pid)
// append request id
if requestId := ctx.Response().Header.Peek(fiber.HeaderXRequestID); len(requestId) > 0 {
_ = bb.WriteByte('-')
_, _ = bb.Write(requestId)
}
_, _ = bb.WriteString(": ")
// append latency
_, _ = bb.WriteString(latency.String())
_ = bb.WriteByte(' ')
// append status code
statusCode := ctx.Response().StatusCode()
bb.B = fasthttp.AppendUint(bb.B, statusCode)
_ = bb.WriteByte(' ')
// append client ip
_, _ = bb.WriteString(ctx.IP())
_ = bb.WriteByte(' ')
// append http method
_, _ = bb.WriteString(ctx.Method())
_ = bb.WriteByte(' ')
// append http protocol://host/uri
_, _ = bb.WriteString(ctx.Protocol())
_, _ = bb.WriteString("://")
_, _ = bb.Write(ctx.Request().URI().Host())
_, _ = bb.Write(ctx.Request().RequestURI())
w := os.Stdout
// append error
if err != nil {
_ = bb.WriteByte(' ')
_, _ = bb.WriteString(err.Error())
w = os.Stderr
}
// append newline
_ = bb.WriteByte('\n')
// ignore error on purpose
_, _ = bb.WriteTo(w)
return
}
}