forked from gramework/gramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
97 lines (84 loc) · 2.57 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
package gramework
import (
"errors"
"github.com/valyala/fasthttp"
)
var (
// ErrEmptyMiddleware can be returned by App.Use*, if middleware is nil
ErrEmptyMiddleware = errors.New("can't use nil middleware")
// ErrUnsupportedMiddlewareType can be returned by App.Use*, if middleware type is unsupported
ErrUnsupportedMiddlewareType = errors.New("unsupported middleware type")
)
// CORSMiddleware provides gramework handler with ctx.CORS() call
func (app *App) CORSMiddleware(domains ...string) func(*Context) {
return func(ctx *Context) {
ctx.CORS(domains...)
}
}
// Use the middleware before request processing
func (app *App) Use(middleware interface{}) error {
if middleware == nil {
return ErrEmptyMiddleware
}
processor, err := app.middlewareProcessor(middleware)
app.middlewaresMu.Lock()
if err == nil {
app.middlewares = append(app.middlewares, processor)
}
app.middlewaresMu.Unlock()
return err
}
// UsePre registers middleware before any other middleware. Use only for metrics or access control!
func (app *App) UsePre(middleware interface{}) error {
if middleware == nil {
return ErrEmptyMiddleware
}
processor, err := app.middlewareProcessor(middleware)
app.preMiddlewaresMu.Lock()
if err == nil {
app.preMiddlewares = append(app.preMiddlewares, processor)
}
app.preMiddlewaresMu.Unlock()
return err
}
// UseAfterRequest the middleware after request processing
func (app *App) UseAfterRequest(middleware interface{}) error {
if middleware == nil {
return ErrEmptyMiddleware
}
processor, err := app.middlewareProcessor(middleware)
app.middlewaresAfterRequestMu.Lock()
if err == nil {
app.middlewaresAfterRequest = append(app.middlewaresAfterRequest, processor)
}
app.middlewaresAfterRequestMu.Unlock()
return nil
}
func (app *App) middlewareProcessor(middleware interface{}) (func(*Context), error) {
// we can register middlewares slowly to serve requests faster
switch m := middleware.(type) {
case func():
return func(*Context) {
m()
}, nil
case func(*Context):
// if middleware is that type, we can just return
// the middleware itself, to save some resources
// required to run the function via closures
return m, nil
case func(*Context) error:
return func(ctx *Context) {
if err := m(ctx); err != nil {
// if error occurred, we can stop processing even slowly
ctx.Logger.Errorf("Middleware error: %s", err)
ctx.Err500()
ctx.middlewaresShouldStopProcessing = true
}
}, nil
case func(*fasthttp.RequestCtx):
return func(ctx *Context) {
m(ctx.RequestCtx)
}, nil
}
return nil, ErrUnsupportedMiddlewareType
}