You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.5 KiB

  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. package cmd
  17. import (
  18. "net/http"
  19. "os"
  20. "strings"
  21. router "github.com/gorilla/mux"
  22. )
  23. func newObjectLayerFn() ObjectLayer {
  24. globalObjLayerMutex.Lock()
  25. defer globalObjLayerMutex.Unlock()
  26. return globalObjectAPI
  27. }
  28. // newObjectLayer - initialize any object layer depending on the number of disks.
  29. func newObjectLayer(storageDisks []StorageAPI) (ObjectLayer, error) {
  30. var objAPI ObjectLayer
  31. var err error
  32. if len(storageDisks) == 1 {
  33. // Initialize FS object layer.
  34. objAPI, err = newFSObjects(storageDisks[0])
  35. } else {
  36. // Initialize XL object layer.
  37. objAPI, err = newXLObjects(storageDisks)
  38. }
  39. if err != nil {
  40. return nil, err
  41. }
  42. // The following actions are performed here, so that any
  43. // requests coming in early in the bootup sequence don't fail
  44. // unexpectedly - e.g. if initEventNotifier was initialized
  45. // after this function completes, an event could be generated
  46. // before the notification system is ready, causing event
  47. // drops or crashes.
  48. // Migrate bucket policy from configDir to .minio.sys/buckets/
  49. err = migrateBucketPolicyConfig(objAPI)
  50. if err != nil {
  51. errorIf(err, "Unable to migrate bucket policy from config directory")
  52. return nil, err
  53. }
  54. err = cleanupOldBucketPolicyConfigs()
  55. if err != nil {
  56. errorIf(err, "Unable to clean up bucket policy from config directory.")
  57. return nil, err
  58. }
  59. // Initialize and load bucket policies.
  60. err = initBucketPolicies(objAPI)
  61. fatalIf(err, "Unable to load all bucket policies.")
  62. // Initialize a new event notifier.
  63. err = initEventNotifier(objAPI)
  64. fatalIf(err, "Unable to initialize event notification.")
  65. // Success.
  66. return objAPI, nil
  67. }
  68. // configureServer handler returns final handler for the http server.
  69. func configureServerHandler(srvCmdConfig serverCmdConfig) (http.Handler, error) {
  70. // Initialize router.
  71. mux := router.NewRouter()
  72. // Initialize distributed NS lock.
  73. if globalIsDistXL {
  74. // Register storage rpc router only if its a distributed setup.
  75. err := registerStorageRPCRouters(mux, srvCmdConfig)
  76. if err != nil {
  77. return nil, err
  78. }
  79. // Register distributed namespace lock.
  80. err = registerDistNSLockRouter(mux, srvCmdConfig)
  81. if err != nil {
  82. return nil, err
  83. }
  84. }
  85. // Register S3 peer communication router.
  86. err := registerS3PeerRPCRouter(mux)
  87. if err != nil {
  88. return nil, err
  89. }
  90. // Register controller rpc router.
  91. err = registerControlRPCRouter(mux, srvCmdConfig)
  92. if err != nil {
  93. return nil, err
  94. }
  95. // set environmental variable MINIO_BROWSER=off to disable minio web browser.
  96. // By default minio web browser is enabled.
  97. if !strings.EqualFold(os.Getenv("MINIO_BROWSER"), "off") {
  98. // Register RPC router for web related calls.
  99. if err = registerBrowserPeerRPCRouter(mux); err != nil {
  100. return nil, err
  101. }
  102. if err = registerWebRouter(mux); err != nil {
  103. return nil, err
  104. }
  105. }
  106. // Add API router.
  107. registerAPIRouter(mux)
  108. // List of some generic handlers which are applied for all incoming requests.
  109. var handlerFns = []HandlerFunc{
  110. // Limits the number of concurrent http requests.
  111. setRateLimitHandler,
  112. // Limits all requests size to a maximum fixed limit
  113. setRequestSizeLimitHandler,
  114. // Adds 'crossdomain.xml' policy handler to serve legacy flash clients.
  115. setCrossDomainPolicy,
  116. // Redirect some pre-defined browser request paths to a static location prefix.
  117. setBrowserRedirectHandler,
  118. // Validates if incoming request is for restricted buckets.
  119. setPrivateBucketHandler,
  120. // Adds cache control for all browser requests.
  121. setBrowserCacheControlHandler,
  122. // CORS setting for all browser API requests.
  123. setCorsHandler,
  124. // Validates all incoming URL resources, for invalid/unsupported
  125. // resources client receives a HTTP error.
  126. setIgnoreResourcesHandler,
  127. // Auth handler verifies incoming authorization headers and
  128. // routes them accordingly. Client receives a HTTP error for
  129. // invalid/unsupported signatures.
  130. setAuthHandler,
  131. // Add new handlers here.
  132. }
  133. // Register rest of the handlers.
  134. return registerHandlers(mux, handlerFns...), nil
  135. }