forked from panco95/go-garden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gin.go
219 lines (195 loc) · 5.2 KB
/
gin.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
package core
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"sync/atomic"
"time"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/jxcia/go-garden/core/log"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func (g *Garden) ginListen(listenAddress string, route func(r *gin.Engine), auth func() gin.HandlerFunc) error {
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
engine.Use(gin.Recovery())
if g.cfg.Service.Debug {
engine.Use(gin.Logger())
if err := createDir(g.cfg.RuntimePath); err != nil {
return err
}
file, err := os.Create(fmt.Sprintf("%s/%s-access.log", g.cfg.RuntimePath, g.cfg.Service.ServiceName))
if err != nil {
return err
}
gin.DefaultWriter = file
engine.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage)
}))
pprof.Register(engine)
} else {
gin.DefaultWriter = ioutil.Discard
}
engine.Use(g.openTracingMiddleware())
if g.cfg.Service.AllowCors {
engine.Use(cors)
}
engine.GET("/metrics", g.prometheus())
if auth != nil {
engine.Use(auth())
}
notFound(engine)
route(engine)
log.Infof("http", "listen on: %s", listenAddress)
return engine.Run(listenAddress)
}
// GatewayRoute create gateway service type to use this
func (g *Garden) GatewayRoute(r *gin.Engine) {
g.serviceType = 1
r.Any("api/:service/*action", func(c *gin.Context) {
g.gateway(c)
})
}
func notFound(r *gin.Engine) {
r.NoRoute(func(c *gin.Context) {
c.JSON(httpNotFound, gatewayFail(infoNotFound))
})
r.NoMethod(func(c *gin.Context) {
c.JSON(httpNotFound, gatewayFail(infoNotFound))
})
}
func (g *Garden) prometheus() gin.HandlerFunc {
h := promhttp.Handler()
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func cors(ctx *gin.Context) {
method := ctx.Request.Method
ctx.Header("Access-Control-Allow-Origin", "*")
ctx.Header("Access-Control-Allow-Headers", "*")
ctx.Header("Access-Control-Allow-Methods", "*")
ctx.Header("Access-Control-Expose-Headers", "*")
ctx.Header("Access-Control-Allow-Credentials", "true")
if method == "OPTIONS" {
ctx.AbortWithStatus(http.StatusNoContent)
}
}
func (g *Garden) openTracingMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
atomic.AddInt64(&g.requestProcess, 1)
span := StartSpanFromHeader(c.Request.Header, c.Request.RequestURI)
span.SetTag("CallType", "Http")
span.SetTag("ServiceIp", g.GetServiceIp())
span.SetTag("ServiceId", g.GetServiceId())
span.SetTag("Status", "unfinished")
// newbody := getBody(c)
request := req{
getMethod(c),
getUrl(c),
getUrlParam(c),
getClientIp(c),
getHeaders(c),
nil}
s, _ := json.Marshal(&request)
// b, _ := json.Marshal(&newbody)
span.SetTag("Request", string(s))
c.Set("tracerid", span.Tracer()) //debug
c.Set("span", span)
c.Set("request", &request)
// c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(b))
c.Next()
span.SetTag("Status", "finished")
span.Finish()
atomic.AddInt64(&g.requestProcess, -1)
atomic.AddInt64(&g.requestFinish, 1)
}
}
// CheckCallSafeMiddleware from call service safe check
func (g *Garden) CheckCallSafeMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
if !g.checkCallSafe(c.GetHeader("Call-Key")) {
c.JSON(httpNotFound, gatewayFail(infoNoAuth))
c.Abort()
}
}
}
//SetContext set custom context
func SetContext(c *gin.Context, name string, val interface{}) {
c.Set(name, val)
}
//GetContext get custom context
func GetContext(c *gin.Context, name string) (interface{}, error) {
t, success := c.Get(name)
if !success {
return nil, errors.New(name + " is nil")
}
return t, nil
}
//GetRequest get request datatype from context
func GetRequest(c *gin.Context) *req {
t, _ := GetContext(c, "request")
r := t.(*req)
return r
}
// GetSpan get opentracing span from context
func GetSpan(c *gin.Context) opentracing.Span {
t, _ := GetContext(c, "span")
r := t.(opentracing.Span)
return r
}
func getMethod(c *gin.Context) string {
return strings.ToUpper(c.Request.Method)
}
func getClientIp(c *gin.Context) string {
return c.ClientIP()
}
func getBody(c *gin.Context) MapData {
body := MapData{}
h := c.GetHeader("Content-Type")
// 获取表单格式请求参数
if strings.Contains(h, "multipart/form-data") || strings.Contains(h, "application/x-www-form-urlencoded") {
c.PostForm("get_params_bug_fix")
for k, v := range c.Request.PostForm {
body[k] = v[0]
}
} else if strings.Contains(h, "application/json") {
c.BindJSON(&body)
}
return body
}
func getUrl(c *gin.Context) string {
return c.Request.URL.Path
}
func getUrlParam(c *gin.Context) string {
requestUrl := c.Request.RequestURI
urlSplit := strings.Split(requestUrl, "?")
if len(urlSplit) > 1 {
requestUrl = "?" + urlSplit[1]
} else {
requestUrl = ""
}
return requestUrl
}
func getHeaders(c *gin.Context) MapData {
headers := MapData{}
for k, v := range c.Request.Header {
headers[k] = v[0]
}
return headers
}