middleware.go (2619B)
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 for _, route := range r.routes { 62 var match RouteMatch 63 if route.Match(req, &match) || match.MatchErr == ErrMethodMismatch { 64 methods, err := route.GetMethods() 65 if err != nil { 66 return nil, err 67 } 68 69 allMethods = append(allMethods, methods...) 70 } 71 } 72 73 return allMethods, nil 74 }