github.com/m3db/m3@v1.5.0/src/query/api/v1/middleware/middleware.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package middleware contains HTTP middleware functions.
    22  package middleware
    23  
    24  import (
    25  	"net/http"
    26  
    27  	"github.com/gorilla/mux"
    28  	"github.com/jonboulle/clockwork"
    29  	"github.com/opentracing/opentracing-go"
    30  	"github.com/prometheus/prometheus/util/httputil"
    31  
    32  	"github.com/m3db/m3/src/x/instrument"
    33  	"github.com/m3db/m3/src/x/net/http/cors"
    34  )
    35  
    36  // Register is a func to build the set of middleware functions.
    37  type Register func(opts Options) []mux.MiddlewareFunc
    38  
    39  // Options is the set of parameters passed to the Register function.
    40  type Options struct {
    41  	// Common options for all middleware functions.
    42  	InstrumentOpts instrument.Options
    43  	Clock          clockwork.Clock
    44  	Route          *mux.Route
    45  
    46  	// Specific options for middleware functions.
    47  	Logging                LoggingOptions
    48  	Metrics                MetricsOptions
    49  	Source                 SourceOptions
    50  	PrometheusRangeRewrite PrometheusRangeRewriteOptions
    51  }
    52  
    53  // OverrideOptions is a function that returns new Options from the provided Options.
    54  type OverrideOptions func(opts Options) Options
    55  
    56  // Default is the default list of middleware functions applied if no middleware functions are set in the
    57  // HandlerOptions.
    58  func Default(opts Options) []mux.MiddlewareFunc {
    59  	// The order of middleware is important. Be very careful when reordering existing middleware.
    60  	return []mux.MiddlewareFunc{
    61  		Cors(),
    62  		// install tracing before logging so the trace_id is available for response logging.
    63  		Tracing(opentracing.GlobalTracer(), opts.InstrumentOpts),
    64  		// install source before logging so the source is available for response logging.
    65  		Source(opts),
    66  		RequestID(opts.InstrumentOpts),
    67  		PrometheusRangeRewrite(opts),
    68  		ResponseLogging(opts),
    69  		ResponseMetrics(opts),
    70  		// install panic handler after any middleware that adds extra useful information to the context logger.
    71  		Panic(opts.InstrumentOpts),
    72  		Compression(),
    73  	}
    74  }
    75  
    76  // Cors adds CORS headers will be added to all responses.
    77  func Cors() mux.MiddlewareFunc {
    78  	return func(base http.Handler) http.Handler {
    79  		return &cors.Handler{
    80  			Handler: base,
    81  			Info: &cors.Info{
    82  				"*": true,
    83  			},
    84  		}
    85  	}
    86  }
    87  
    88  // Compression adds suitable response compression based on the client's Accept-Encoding headers.
    89  func Compression() mux.MiddlewareFunc {
    90  	return func(base http.Handler) http.Handler {
    91  		return httputil.CompressionHandler{
    92  			Handler: base,
    93  		}
    94  	}
    95  }