storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/routers.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2015, 2016 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"net/http"
    21  
    22  	"github.com/gorilla/mux"
    23  )
    24  
    25  // Composed function registering routers for only distributed Erasure setup.
    26  func registerDistErasureRouters(router *mux.Router, endpointServerPools EndpointServerPools) {
    27  	// Register storage REST router only if its a distributed setup.
    28  	registerStorageRESTHandlers(router, endpointServerPools)
    29  
    30  	// Register peer REST router only if its a distributed setup.
    31  	registerPeerRESTHandlers(router)
    32  
    33  	// Register bootstrap REST router for distributed setups.
    34  	registerBootstrapRESTHandlers(router)
    35  
    36  	// Register distributed namespace lock routers.
    37  	registerLockRESTHandlers(router)
    38  }
    39  
    40  // List of some generic handlers which are applied for all incoming requests.
    41  var GlobalHandlers = []mux.MiddlewareFunc{
    42  	// filters HTTP headers which are treated as metadata and are reserved
    43  	// for internal use only.
    44  	filterReservedMetadata,
    45  	// Enforce rules specific for TLS requests
    46  	setSSETLSHandler,
    47  	// Auth handler verifies incoming authorization headers and
    48  	// routes them accordingly. Client receives a HTTP error for
    49  	// invalid/unsupported signatures.
    50  	setAuthHandler,
    51  	// Validates all incoming requests to have a valid date header.
    52  	setTimeValidityHandler,
    53  	// Adds cache control for all browser requests.
    54  	setBrowserCacheControlHandler,
    55  	// Validates if incoming request is for restricted buckets.
    56  	setReservedBucketHandler,
    57  	// Redirect some pre-defined browser request paths to a static location prefix.
    58  	setBrowserRedirectHandler,
    59  	// Adds 'crossdomain.xml' policy handler to serve legacy flash clients.
    60  	setCrossDomainPolicy,
    61  	// Limits all header sizes to a maximum fixed limit
    62  	setRequestHeaderSizeLimitHandler,
    63  	// Limits all requests size to a maximum fixed limit
    64  	setRequestSizeLimitHandler,
    65  	// Network statistics
    66  	setHTTPStatsHandler,
    67  	// Validate all the incoming requests.
    68  	setRequestValidityHandler,
    69  	// set HTTP security headers such as Content-Security-Policy.
    70  	addSecurityHeaders,
    71  	// set x-amz-request-id header.
    72  	addCustomHeaders,
    73  	// add redirect handler to redirect
    74  	// requests when object layer is not
    75  	// initialized.
    76  	setRedirectHandler,
    77  	// Add new handlers here.
    78  }
    79  
    80  // configureServer handler returns final handler for the http server.
    81  func configureServerHandler(endpointServerPools EndpointServerPools) (http.Handler, error) {
    82  	// Initialize router. `SkipClean(true)` stops gorilla/mux from
    83  	// normalizing URL path minio/minio#3256
    84  	router := mux.NewRouter().SkipClean(true).UseEncodedPath()
    85  
    86  	// Initialize distributed NS lock.
    87  	if globalIsDistErasure {
    88  		registerDistErasureRouters(router, endpointServerPools)
    89  	}
    90  
    91  	// Register web router when its enabled.
    92  	if globalBrowserEnabled {
    93  		if err := registerWebRouter(router); err != nil {
    94  			return nil, err
    95  		}
    96  	}
    97  
    98  	// Add Admin router, all APIs are enabled in server mode.
    99  	registerAdminRouter(router, true, true)
   100  
   101  	// Add healthcheck router
   102  	registerHealthCheckRouter(router)
   103  
   104  	// Add server metrics router
   105  	registerMetricsRouter(router)
   106  
   107  	// Add STS router always.
   108  	registerSTSRouter(router)
   109  
   110  	// Add API router
   111  	registerAPIRouter(router)
   112  
   113  	router.Use(GlobalHandlers...)
   114  
   115  	return router, nil
   116  }