-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.go
78 lines (67 loc) · 2.18 KB
/
handler.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
package middleware
import (
"net/http"
sentryecho "github.com/getsentry/sentry-go/echo"
"github.com/labstack/echo/v4"
)
// CORS allows certain CORS headers.
func CORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Authorization2, Origin, X-Requested-With, Content-Type, Accept, Platform, Version, X-Request-ID")
if r.Method == "OPTIONS" {
return
}
next.ServeHTTP(w, r)
})
}
// XRequestID add X-Request-ID header if not exists.
func XRequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestId := r.Header.Get(echo.HeaderXRequestID)
if !isValidXRequestID(requestId) {
requestId = generateXRequestID()
r.Header.Add(echo.HeaderXRequestID, requestId)
}
w.Header().Set(echo.HeaderXRequestID, requestId)
next.ServeHTTP(w, r)
})
}
// EchoHandler is wrapper for Echo.
func EchoHandler(next http.Handler) echo.HandlerFunc {
return func(ctx echo.Context) error {
ctx = applySentryHubToContext(ctx)
ctx = applyIpToContext(ctx)
req := ctx.Request()
CORS(XRequestID(next)).ServeHTTP(ctx.Response(), req)
return nil
}
}
// EchoSentryHubContext middleware applies sentry hub to context for zenrpc middleware.
func EchoSentryHubContext() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return next(applySentryHubToContext(c))
}
}
}
// EchoIPContext middleware applies client ip to context for zenrpc middleware.
func EchoIPContext() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return next(applyIpToContext(c))
}
}
}
func applySentryHubToContext(c echo.Context) echo.Context {
if hub := sentryecho.GetHubFromContext(c); hub != nil {
req := c.Request()
c.SetRequest(req.WithContext(NewSentryHubContext(req.Context(), hub)))
}
return c
}
func applyIpToContext(c echo.Context) echo.Context {
req := c.Request()
c.SetRequest(req.WithContext(NewIPContext(req.Context(), c.RealIP())))
return c
}