gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/mux/middleware.go (about)

     1  package mux
     2  
     3  import (
     4  	"strings"
     5  
     6  	http "gitee.com/ks-custle/core-gm/gmhttp"
     7  )
     8  
     9  // MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler.
    10  // Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed
    11  // to it, and then calls the handler passed as parameter to the MiddlewareFunc.
    12  type MiddlewareFunc func(http.Handler) http.Handler
    13  
    14  // middleware interface is anything which implements a MiddlewareFunc named Middleware.
    15  type middleware interface {
    16  	Middleware(handler http.Handler) http.Handler
    17  }
    18  
    19  // Middleware allows MiddlewareFunc to implement the middleware interface.
    20  func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler {
    21  	return mw(handler)
    22  }
    23  
    24  // 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.
    25  func (r *Router) Use(mwf ...MiddlewareFunc) {
    26  	for _, fn := range mwf {
    27  		r.middlewares = append(r.middlewares, fn)
    28  	}
    29  }
    30  
    31  // 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.
    32  func (r *Router) useInterface(mw middleware) {
    33  	r.middlewares = append(r.middlewares, mw)
    34  }
    35  
    36  // CORSMethodMiddleware automatically sets the Access-Control-Allow-Methods response header
    37  // on requests for routes that have an OPTIONS method matcher to all the method matchers on
    38  // the route. Routes that do not explicitly handle OPTIONS requests will not be processed
    39  // by the middleware. See examples for usage.
    40  func CORSMethodMiddleware(r *Router) MiddlewareFunc {
    41  	return func(next http.Handler) http.Handler {
    42  		return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    43  			allMethods, err := getAllMethodsForRoute(r, req)
    44  			if err == nil {
    45  				for _, v := range allMethods {
    46  					if v == http.MethodOptions {
    47  						w.Header().Set("Access-Control-Allow-Methods", strings.Join(allMethods, ","))
    48  					}
    49  				}
    50  			}
    51  
    52  			next.ServeHTTP(w, req)
    53  		})
    54  	}
    55  }
    56  
    57  // getAllMethodsForRoute returns all the methods from method matchers matching a given
    58  // request.
    59  func getAllMethodsForRoute(r *Router, req *http.Request) ([]string, error) {
    60  	var allMethods []string
    61  
    62  	for _, route := range r.routes {
    63  		var match RouteMatch
    64  		if route.Match(req, &match) || match.MatchErr == ErrMethodMismatch {
    65  			methods, err := route.GetMethods()
    66  			if err != nil {
    67  				return nil, err
    68  			}
    69  
    70  			allMethods = append(allMethods, methods...)
    71  		}
    72  	}
    73  
    74  	return allMethods, nil
    75  }