From e509bcb2b959bd6b974dba8296764ecd48140779 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Wed, 24 Feb 2016 00:07:27 +0530 Subject: [PATCH] UI: serve index.html if the requested file is not found in the assets bundle. --- generic-handlers.go | 2 +- routers.go | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/generic-handlers.go b/generic-handlers.go index 82d6cae54..a36c60b2a 100644 --- a/generic-handlers.go +++ b/generic-handlers.go @@ -104,7 +104,7 @@ func (h redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // '/rpc' is redirected to 'locationPrefix/rpc' // '/login' is redirected to 'locationPrefix/login' switch r.URL.Path { - case "/", "/rpc", "/login": + case "/", "/rpc", "/login", "/favicon.ico": location := h.locationPrefix + r.URL.Path // Redirect to new location. http.Redirect(w, r, location, http.StatusTemporaryRedirect) diff --git a/routers.go b/routers.go index 8ccc85e67..918caead8 100644 --- a/routers.go +++ b/routers.go @@ -19,7 +19,6 @@ package main import ( "net" "net/http" - "path" router "github.com/gorilla/mux" jsonrpc "github.com/gorilla/rpc/v2" @@ -58,6 +57,16 @@ type webAPI struct { secretAccessKey string } +// 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 = privateBucket + "/" + h.handler.ServeHTTP(w, r) +} + // registerAPIHandlers - register all the handlers to their respective paths func registerAPIHandlers(mux *router.Router, a storageAPI, w *webAPI) { // Minio rpc router @@ -72,10 +81,10 @@ func registerAPIHandlers(mux *router.Router, a storageAPI, w *webAPI) { // RPC handler at URI - /minio/rpc minio.Path("/rpc").Handler(rpc) - - // Web handler assets at URI - /minio/login - minio.Path("/login").Handler(http.StripPrefix(path.Join(privateBucket, "login"), http.FileServer(assetFS()))) - minio.Path("/{file:.*}").Handler(http.StripPrefix(privateBucket, http.FileServer(assetFS()))) + // Serve javascript files and favicon.ico from assets + minio.Path("/{assets:[^/]+.js|favicon.ico}").Handler(http.StripPrefix(privateBucket, http.FileServer(assetFS()))) + // Serve index.html for rest of the requests + minio.Path("/{index:.*}").Handler(indexHandler{http.StripPrefix(privateBucket, http.FileServer(assetFS()))}) // API Router api := mux.NewRoute().PathPrefix("/").Subrouter()