-
Notifications
You must be signed in to change notification settings - Fork 17
/
middleware.go
27 lines (23 loc) · 1.02 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
package mps
import "net/http"
// Middleware will "tamper" with the request coming to the proxy server
type Middleware interface {
// Handle execute the next middleware as a linked list. "ctx.Next(req)"
// eg:
// func Handle(req *http.Request, ctx *Context) (*http.Response, error) {
// // You can do anything to modify the http.Request ...
// resp, err := ctx.Next(req)
// // You can do anything to modify the http.Response ...
// return resp, err
// }
//
// Alternatively, you can simply return the response without executing `ctx.Next()`,
// which will interrupt subsequent middleware execution.
Handle(req *http.Request, ctx *Context) (*http.Response, error)
}
// MiddlewareFunc A wrapper that would convert a function to a Middleware interface type
type MiddlewareFunc func(req *http.Request, ctx *Context) (*http.Response, error)
// Handle Middleware.Handle(req, ctx) <=> MiddlewareFunc(req, ctx)
func (f MiddlewareFunc) Handle(req *http.Request, ctx *Context) (*http.Response, error) {
return f(req, ctx)
}