-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathginx.go
151 lines (133 loc) · 2.88 KB
/
ginx.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
package ginx
import (
"context"
"github.com/gin-gonic/gin"
"github.com/pinealctx/neptune/ulog"
"go.uber.org/zap"
"net/http"
"time"
)
const (
CookieKey = "api_token"
)
type GinGroup struct {
*gin.RouterGroup
*GinX
}
type GinX struct {
*gin.Engine
srv *http.Server
cookieAge int
cookieKey string
}
func New(addr string, optFns ...OptionFn) *GinX {
var opt = &option{}
*opt = defaultOpt
for _, optFn := range optFns {
optFn(opt)
}
var s = &GinX{}
s.cookieAge = opt.cookieAge
s.cookieKey = opt.cookieKey
s.srv = &http.Server{Addr: addr}
s.Engine = gin.New()
if opt.recoverable {
s.Engine.Use(recovery)
}
if opt.logRequest {
s.Engine.Use(logRequest)
}
for _, middleware := range opt.middlewares {
s.Engine.Use(middleware)
}
return s
}
func (s *GinX) Serve() error {
s.srv.Handler = s.Engine
return s.srv.ListenAndServe()
}
func (s *GinX) Run(errChan chan error) {
go func() {
errChan <- s.Serve()
}()
}
func (s *GinX) Stop() error {
return s.srv.Close()
}
func (s *GinX) Shutdown(ctx context.Context) error {
return s.srv.Shutdown(ctx)
}
func (s *GinX) GinGroup(relativePath string, handlers ...gin.HandlerFunc) *GinGroup {
return &GinGroup{
RouterGroup: s.Group(relativePath, handlers...),
GinX: s,
}
}
func (s *GinX) SetCookie(c *gin.Context, val string) {
var ck = &http.Cookie{
Name: s.cookieKey,
Value: val,
Path: "/",
MaxAge: s.cookieAge,
HttpOnly: true,
}
c.Writer.Header().Set("Set-Cookie", ck.String())
}
func (s *GinX) GetCookie(c *gin.Context) (string, error) {
var ck, err = c.Request.Cookie(s.cookieKey)
if err != nil {
return "", err
}
return ck.Value, nil
}
func (s *GinX) GetToken(c *gin.Context) string {
var tk = c.Request.Header.Get(s.cookieKey)
if tk != "" {
return tk
}
var ck, err = s.GetCookie(c)
if err != nil {
return ""
}
return ck
}
func (s *GinX) ClearCookie(c *gin.Context) {
var ck = &http.Cookie{
Name: s.cookieKey,
MaxAge: -1,
Path: "/",
Secure: true,
HttpOnly: true,
}
c.Writer.Header().Set("Set-Cookie", ck.String())
}
func recovery(c *gin.Context) {
defer func() {
var capture = recover()
if capture != nil {
c.AbortWithStatus(http.StatusInternalServerError)
ulog.Error("http.panic", ctxFields(c, zap.Reflect("capture", capture), zap.Stack("stack"))...)
}
}()
c.Next()
}
func logRequest(c *gin.Context) {
var start = time.Now()
c.Next()
var end = time.Now()
ulog.Debug("http.request", ctxFields(c, zap.Time("end", end), zap.Duration("latency", end.Sub(start)))...)
}
func ctxFields(c *gin.Context, fields ...zap.Field) []zap.Field {
var dist = []zap.Field{
zap.String("path", c.Request.URL.Path),
zap.String("method", c.Request.Method),
zap.Int("status", c.Writer.Status()),
zap.String("ip", c.ClientIP()),
zap.String("ua", c.Request.UserAgent()),
}
dist = append(dist, fields...)
return dist
}
func init() {
gin.SetMode(gin.ReleaseMode)
}