-
Notifications
You must be signed in to change notification settings - Fork 405
/
middleware.go
122 lines (103 loc) · 3.97 KB
/
middleware.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
package server
import (
"context"
"net/http"
"github.com/fnproject/fn/api/common"
"github.com/fnproject/fn/fnext"
"github.com/gin-gonic/gin"
)
type middlewareController struct {
// NOTE: I tried to make this work as if it were a normal context, but it just doesn't work right. If someone
// does something like context.WithValue, then the return is a new &context.valueCtx{} which can't be cast. So now stuffing it into a value instead.
// context.Context
// separating this out so we can use it and don't have to reimplement context.Context above
ginContext *gin.Context
server *Server
}
func (s *Server) apiMiddlewareWrapper() gin.HandlerFunc {
return func(c *gin.Context) {
// fmt.Println("api middleware")
s.runMiddleware(c, s.apiMiddlewares)
}
}
func (s *Server) rootMiddlewareWrapper() gin.HandlerFunc {
return func(c *gin.Context) {
// fmt.Println("ROOT MIDDLE")
s.runMiddleware(c, s.rootMiddlewares)
}
}
// This is basically a single gin middleware that runs a bunch of fn middleware.
// The final handler will pass it back to gin for further processing.
func (s *Server) runMiddleware(c *gin.Context, ms []fnext.Middleware) {
// fmt.Println("runMiddleware")
if len(ms) == 0 {
// fmt.Println("ZERO MIDDLEWARE")
c.Next()
return
}
defer func() {
//This is so that if the server errors or panics on a middleware the server will still respond and not send eof to client.
err := recover()
if err != nil {
common.Logger(c.Request.Context()).WithField("MiddleWarePanicRecovery:", err).Errorln("A panic occurred during middleware.")
handleErrorResponse(c, ErrInternalServerError)
}
}()
ctx := context.WithValue(c.Request.Context(), fnext.MiddlewareControllerKey, s.newMiddlewareController(c))
last := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// fmt.Println("final handler called")
ctx := r.Context()
c.Request = r.WithContext(ctx)
c.Next()
})
chainAndServe(ms, c.Writer, c.Request.WithContext(ctx), last)
c.Abort() // we always abort here because the middleware decides to call next or not. If the `last` handler gets called, it will continue, otherwise we abort.
}
func (s *Server) newMiddlewareController(c *gin.Context) *middlewareController {
return &middlewareController{
ginContext: c,
server: s,
}
}
// TODO: I will remove this and other debug commented lines once I'm sure it's all right.
func debugH(i int, m fnext.Middleware, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// fmt.Println("handling", i, "m:", reflect.TypeOf(m), "h:", reflect.TypeOf(h))
h.ServeHTTP(w, r) // call original
})
}
// chainAndServe essentially makes a chain of middleware wrapped around each other, then calls ServerHTTP on the end result.
// then each middleware also calls ServeHTTP within it
func chainAndServe(ms []fnext.Middleware, w http.ResponseWriter, r *http.Request, last http.Handler) {
h := last
// These get chained in reverse order so they play out in the right order. Don't ask.
for i := len(ms) - 1; i >= 0; i-- {
m := ms[i]
h = m.Handle(debugH(i, m, h))
}
h.ServeHTTP(w, r)
}
// AddMiddleware DEPRECATED - see AddAPIMiddleware
func (s *Server) AddMiddleware(m fnext.Middleware) {
s.AddAPIMiddleware(m)
}
// AddMiddlewareFunc DEPRECATED - see AddAPIMiddlewareFunc
func (s *Server) AddMiddlewareFunc(m fnext.MiddlewareFunc) {
s.AddAPIMiddlewareFunc(m)
}
// AddAPIMiddleware add middleware
func (s *Server) AddAPIMiddleware(m fnext.Middleware) {
s.apiMiddlewares = append(s.apiMiddlewares, m)
}
// AddAPIMiddlewareFunc add middlewarefunc
func (s *Server) AddAPIMiddlewareFunc(m fnext.MiddlewareFunc) {
s.AddAPIMiddleware(m)
}
// AddRootMiddleware add middleware add middleware for end user applications
func (s *Server) AddRootMiddleware(m fnext.Middleware) {
s.rootMiddlewares = append(s.rootMiddlewares, m)
}
// AddRootMiddlewareFunc add middleware for end user applications
func (s *Server) AddRootMiddlewareFunc(m fnext.MiddlewareFunc) {
s.AddRootMiddleware(m)
}