-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.go
More file actions
131 lines (115 loc) · 2.76 KB
/
Copy pathmiddleware.go
File metadata and controls
131 lines (115 loc) · 2.76 KB
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
package fwncs
import (
"context"
"fmt"
"math/rand"
"net/http"
"runtime"
"time"
"github.com/n-creativesystem/go-fwncs/constant"
)
type DefaultResponseBody struct {
Code int `json:"code"`
Status string `json:"status"`
Message string `json:"message"`
Internal error `json:"-"`
}
func (d *DefaultResponseBody) Error() string {
if d.Internal == nil {
return fmt.Sprintf("code=%d, message=%v", d.Code, d.Message)
}
return fmt.Sprintf("code=%d, message=%v, internal=%v", d.Code, d.Message, d.Internal)
}
func NewDefaultResponseBody(code int, message string) *DefaultResponseBody {
var status string
// 200 ~ 226
switch {
case http.StatusOK <= code && code <= http.StatusIMUsed:
status = "success"
case http.StatusMultipleChoices <= code && code <= http.StatusPermanentRedirect:
status = "redirect"
default:
status = "error"
}
if http.StatusOK <= code && code <= http.StatusIMUsed {
status = "success"
} else {
status = "error"
}
return &DefaultResponseBody{
Status: status,
Code: code,
Message: message,
}
}
func Recovery() HandlerFunc {
return func(c Context) {
defer func() {
if rcv := recover(); rcv != nil {
for depth := 0; ; depth++ {
_, file, line, ok := runtime.Caller(depth)
if !ok {
break
}
c.Logger().Error(fmt.Sprintf("%d: %v:%d", depth, file, line))
}
c.AbortWithStatusAndMessage(http.StatusInternalServerError, NewDefaultResponseBody(http.StatusInternalServerError, fmt.Sprintf("%v", rcv)))
}
}()
c.Next()
}
}
var requestIDKey fwNscContext
func FromRequestID(ctx context.Context) string {
rid, ok := ctx.Value(requestIDKey).(string)
if !ok {
rid = requestIDGenerator()
}
return rid
}
type RequestIDConfig struct {
Generator func() string
}
func RequestID() HandlerFunc {
return RequestIDWithConfig(RequestIDConfig{
Generator: requestIDGenerator,
})
}
func RequestIDWithConfig(config RequestIDConfig) HandlerFunc {
if config.Generator == nil {
config.Generator = requestIDGenerator
}
return func(c Context) {
rid := c.GetRequestID()
if rid == "" {
rid = config.Generator()
}
ctx := c.GetContext()
ctx = context.WithValue(ctx, requestIDKey, rid)
c.SetContext(ctx)
c.SetHeader(constant.HeaderXRequestID, rid)
idCookie := new(http.Cookie)
idCookie.Path = "/"
idCookie.Name = constant.HeaderXRequestID
idCookie.Value = rid
idCookie.MaxAge = 0
c.SetCookie(idCookie)
c.Next()
}
}
type Random struct{}
var random = newRandom()
func newRandom() *Random {
rand.Seed(time.Now().UnixNano())
return new(Random)
}
func (*Random) String(length uint8) string {
b := make([]byte, length)
for i := range b {
b[i] = Alphanumeric[rand.Int63()%int64(len(Alphanumeric))]
}
return string(b)
}
func requestIDGenerator() string {
return random.String(32)
}