github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/routers.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"net/http"
    22  
    23  	"github.com/minio/minio/internal/grid"
    24  	"github.com/minio/mux"
    25  )
    26  
    27  // Composed function registering routers for only distributed Erasure setup.
    28  func registerDistErasureRouters(router *mux.Router, endpointServerPools EndpointServerPools) {
    29  	// Register storage REST router only if its a distributed setup.
    30  	registerStorageRESTHandlers(router, endpointServerPools, globalGrid.Load())
    31  
    32  	// Register peer REST router only if its a distributed setup.
    33  	registerPeerRESTHandlers(router, globalGrid.Load())
    34  
    35  	// Register bootstrap REST router for distributed setups.
    36  	registerBootstrapRESTHandlers(globalGrid.Load())
    37  
    38  	// Register distributed namespace lock routers.
    39  	registerLockRESTHandlers()
    40  
    41  	// Add grid to router
    42  	router.Handle(grid.RoutePath, adminMiddleware(globalGrid.Load().Handler(), noGZFlag, noObjLayerFlag))
    43  }
    44  
    45  // List of some generic middlewares which are applied for all incoming requests.
    46  var globalMiddlewares = []mux.MiddlewareFunc{
    47  	// set x-amz-request-id header and others
    48  	addCustomHeadersMiddleware,
    49  	// The generic tracer needs to be the first middleware to catch all requests
    50  	// returned early by any other middleware (but after the middleware that
    51  	// sets the amz request id).
    52  	httpTracerMiddleware,
    53  	// Auth middleware verifies incoming authorization headers and routes them
    54  	// accordingly. Client receives a HTTP error for invalid/unsupported
    55  	// signatures.
    56  	//
    57  	// Validates all incoming requests to have a valid date header.
    58  	setAuthMiddleware,
    59  	// Redirect some pre-defined browser request paths to a static location
    60  	// prefix.
    61  	setBrowserRedirectMiddleware,
    62  	// Adds 'crossdomain.xml' policy middleware to serve legacy flash clients.
    63  	setCrossDomainPolicyMiddleware,
    64  	// Limits all body and header sizes to a maximum fixed limit
    65  	setRequestLimitMiddleware,
    66  	// Validate all the incoming requests.
    67  	setRequestValidityMiddleware,
    68  	// Add upload forwarding middleware for site replication
    69  	setUploadForwardingMiddleware,
    70  	// Add bucket forwarding middleware
    71  	setBucketForwardingMiddleware,
    72  	// Add new middlewares here.
    73  }
    74  
    75  // configureServer handler returns final handler for the http server.
    76  func configureServerHandler(endpointServerPools EndpointServerPools) (http.Handler, error) {
    77  	// Initialize router. `SkipClean(true)` stops minio/mux from
    78  	// normalizing URL path minio/minio#3256
    79  	router := mux.NewRouter().SkipClean(true).UseEncodedPath()
    80  
    81  	// Initialize distributed NS lock.
    82  	if globalIsDistErasure {
    83  		registerDistErasureRouters(router, endpointServerPools)
    84  	}
    85  
    86  	// Add Admin router, all APIs are enabled in server mode.
    87  	registerAdminRouter(router, true)
    88  
    89  	// Add healthCheck router
    90  	registerHealthCheckRouter(router)
    91  
    92  	// Add server metrics router
    93  	registerMetricsRouter(router)
    94  
    95  	// Add STS router always.
    96  	registerSTSRouter(router)
    97  
    98  	// Add KMS router
    99  	registerKMSRouter(router)
   100  
   101  	// Add API router
   102  	registerAPIRouter(router)
   103  
   104  	router.Use(globalMiddlewares...)
   105  
   106  	return router, nil
   107  }