2016-03-27 21:37:21 +02:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-03-27 21:37:21 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/elazarl/go-bindata-assetfs"
|
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
router "github.com/gorilla/mux"
|
|
|
|
jsonrpc "github.com/gorilla/rpc/v2"
|
|
|
|
"github.com/gorilla/rpc/v2/json2"
|
2017-01-24 03:07:22 +01:00
|
|
|
"github.com/minio/minio/browser"
|
2016-03-27 21:37:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// webAPI container for Web API.
|
2016-04-12 21:45:15 +02:00
|
|
|
type webAPIHandlers struct {
|
2016-07-31 23:11:14 +02:00
|
|
|
ObjectAPI func() ObjectLayer
|
2016-03-27 21:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// indexHandler - Handler to serve index.html
|
|
|
|
type indexHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.URL.Path = reservedBucket + "/"
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
const assetPrefix = "production"
|
|
|
|
|
|
|
|
func assetFS() *assetfs.AssetFS {
|
|
|
|
return &assetfs.AssetFS{
|
|
|
|
Asset: miniobrowser.Asset,
|
|
|
|
AssetDir: miniobrowser.AssetDir,
|
|
|
|
AssetInfo: miniobrowser.AssetInfo,
|
|
|
|
Prefix: assetPrefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// specialAssets are files which are unique files not embedded inside index_bundle.js.
|
|
|
|
const specialAssets = "loader.css|logo.svg|firefox.png|safari.png|chrome.png|favicon.ico"
|
|
|
|
|
|
|
|
// registerWebRouter - registers web router for serving minio browser.
|
2016-10-14 02:34:10 +02:00
|
|
|
func registerWebRouter(mux *router.Router) error {
|
2016-10-05 21:48:07 +02:00
|
|
|
// Initialize Web.
|
|
|
|
web := &webAPIHandlers{
|
|
|
|
ObjectAPI: newObjectLayerFn,
|
|
|
|
}
|
|
|
|
|
2016-03-27 21:37:21 +02:00
|
|
|
// Initialize a new json2 codec.
|
|
|
|
codec := json2.NewCodec()
|
|
|
|
|
2016-04-08 08:44:08 +02:00
|
|
|
// Minio browser router.
|
2016-03-27 21:37:21 +02:00
|
|
|
webBrowserRouter := mux.NewRoute().PathPrefix(reservedBucket).Subrouter()
|
|
|
|
|
|
|
|
// Initialize json rpc handlers.
|
|
|
|
webRPC := jsonrpc.NewServer()
|
|
|
|
webRPC.RegisterCodec(codec, "application/json")
|
|
|
|
webRPC.RegisterCodec(codec, "application/json; charset=UTF-8")
|
2016-10-14 02:34:10 +02:00
|
|
|
|
|
|
|
// Register RPC handlers with server
|
|
|
|
if err := webRPC.RegisterService(web, "Web"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-27 21:37:21 +02:00
|
|
|
|
2016-04-08 08:44:08 +02:00
|
|
|
// RPC handler at URI - /minio/webrpc
|
|
|
|
webBrowserRouter.Methods("POST").Path("/webrpc").Handler(webRPC)
|
2016-03-31 15:57:29 +02:00
|
|
|
webBrowserRouter.Methods("PUT").Path("/upload/{bucket}/{object:.+}").HandlerFunc(web.Upload)
|
2016-04-12 21:45:15 +02:00
|
|
|
webBrowserRouter.Methods("GET").Path("/download/{bucket}/{object:.+}").Queries("token", "{token:.*}").HandlerFunc(web.Download)
|
2016-03-27 21:37:21 +02:00
|
|
|
|
|
|
|
// Add compression for assets.
|
|
|
|
compressedAssets := handlers.CompressHandler(http.StripPrefix(reservedBucket, http.FileServer(assetFS())))
|
|
|
|
|
|
|
|
// Serve javascript files and favicon from assets.
|
|
|
|
webBrowserRouter.Path(fmt.Sprintf("/{assets:[^/]+.js|%s}", specialAssets)).Handler(compressedAssets)
|
|
|
|
|
|
|
|
// Serve index.html for rest of the requests.
|
|
|
|
webBrowserRouter.Path("/{index:.*}").Handler(indexHandler{http.StripPrefix(reservedBucket, http.FileServer(assetFS()))})
|
2016-10-14 02:34:10 +02:00
|
|
|
|
|
|
|
return nil
|
2016-03-27 21:37:21 +02:00
|
|
|
}
|