Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame^] | 1 | package mux |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler. |
| 9 | // Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed |
| 10 | // to it, and then calls the handler passed as parameter to the MiddlewareFunc. |
| 11 | type MiddlewareFunc func(http.Handler) http.Handler |
| 12 | |
| 13 | // middleware interface is anything which implements a MiddlewareFunc named Middleware. |
| 14 | type middleware interface { |
| 15 | Middleware(handler http.Handler) http.Handler |
| 16 | } |
| 17 | |
| 18 | // Middleware allows MiddlewareFunc to implement the middleware interface. |
| 19 | func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler { |
| 20 | return mw(handler) |
| 21 | } |
| 22 | |
| 23 | // Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router. |
| 24 | func (r *Router) Use(mwf ...MiddlewareFunc) { |
| 25 | for _, fn := range mwf { |
| 26 | r.middlewares = append(r.middlewares, fn) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // useInterface appends a middleware to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router. |
| 31 | func (r *Router) useInterface(mw middleware) { |
| 32 | r.middlewares = append(r.middlewares, mw) |
| 33 | } |
| 34 | |
| 35 | // CORSMethodMiddleware automatically sets the Access-Control-Allow-Methods response header |
| 36 | // on requests for routes that have an OPTIONS method matcher to all the method matchers on |
| 37 | // the route. Routes that do not explicitly handle OPTIONS requests will not be processed |
| 38 | // by the middleware. See examples for usage. |
| 39 | func CORSMethodMiddleware(r *Router) MiddlewareFunc { |
| 40 | return func(next http.Handler) http.Handler { |
| 41 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 42 | allMethods, err := getAllMethodsForRoute(r, req) |
| 43 | if err == nil { |
| 44 | for _, v := range allMethods { |
| 45 | if v == http.MethodOptions { |
| 46 | w.Header().Set("Access-Control-Allow-Methods", strings.Join(allMethods, ",")) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | next.ServeHTTP(w, req) |
| 52 | }) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // getAllMethodsForRoute returns all the methods from method matchers matching a given |
| 57 | // request. |
| 58 | func getAllMethodsForRoute(r *Router, req *http.Request) ([]string, error) { |
| 59 | var allMethods []string |
| 60 | |
| 61 | err := r.Walk(func(route *Route, _ *Router, _ []*Route) error { |
| 62 | for _, m := range route.matchers { |
| 63 | if _, ok := m.(*routeRegexp); ok { |
| 64 | if m.Match(req, &RouteMatch{}) { |
| 65 | methods, err := route.GetMethods() |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | allMethods = append(allMethods, methods...) |
| 71 | } |
| 72 | break |
| 73 | } |
| 74 | } |
| 75 | return nil |
| 76 | }) |
| 77 | |
| 78 | return allMethods, err |
| 79 | } |