-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmiddleware.go
38 lines (32 loc) · 1.16 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
package pulse
type MiddlewareFunc func(handler Handler) Handler
type Middleware interface {
Middleware(handler Handler) Handler
Handle(ctx *Context, next Handler) error
}
func (m MiddlewareFunc) Middleware(handler Handler) Handler {
return m(handler)
}
func (r *Router) Use(method string, middlewares ...interface{}) {
for _, middleware := range middlewares {
if middlewareFunc, ok := middleware.(MiddlewareFunc); ok {
r.middlewares[method] = append(r.middlewares[method], middlewareFunc)
} else if middleware, ok := middleware.(Middleware); ok {
r.middlewares[method] = append(r.middlewares[method], middleware)
}
}
}
func CORSMiddleware() MiddlewareFunc {
return func(handler Handler) Handler {
return func(ctx *Context) error {
ctx.SetResponseHeader("Access-Control-Allow-Origin", "*")
ctx.SetResponseHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
ctx.SetResponseHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
return handler(ctx)
}
}
}
func (m MiddlewareFunc) Handle(ctx *Context, next Handler) error {
h := m(next)
return h(ctx)
}