Skip to content

Commit

Permalink
Merge pull request rancher#2 from rancher-sandbox/feature/local-dashb…
Browse files Browse the repository at this point in the history
…oard

Serve Rancher Dashboard as SPA with local builds
  • Loading branch information
rak-phillip authored Feb 22, 2022
2 parents ded0f70 + 2749507 commit 6677656
Showing 1 changed file with 67 additions and 6 deletions.
73 changes: 67 additions & 6 deletions pkg/ui/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,56 @@ package ui

import (
"net/http"
"os"
"path/filepath"
"strings"

"github.com/gorilla/mux"
)

// spaHandler implements the http.Handler interface, so we can use it
// to respond to HTTP requests. The path to the static directory and
// path to the index file within that static directory are used to
// serve the SPA in the given static directory.
type spaHandler struct {
staticPath string
indexPath string
}

// ServeHTTP inspects the URL path to locate a file within the static dir
// on the SPA handler. If a file is found, it will be served. If not, the
// file located at the index path on the SPA handler will be served. This
// is suitable behavior for serving an SPA (single page application).
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// get the absolute path to prevent directory traversal
path, err := filepath.Abs(r.URL.Path)
if err != nil {
// if we failed to get the absolute path respond with a 400 bad request
// and stop
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

// prepend the path with the path to the static directory
path = filepath.Join(h.staticPath, path)

// check whether a file exists at the given path
_, err = os.Stat(path)
if os.IsNotExist(err) {
// file does not exist, serve index.html
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
return
} else if err != nil {
// if we got an error (that wasn't that the file doesn't exist) stating the
// file, return a 500 internal server error and stop
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// otherwise, use http.FileServer to serve the static dir
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}

func New(path string, offline string) http.Handler {
vue := NewUIHandler(&Options{
Path: func() string {
Expand All @@ -30,12 +75,6 @@ func New(path string, offline string) http.Handler {
router := mux.NewRouter()
router.UseEncodedPath()

router.Handle("/", http.RedirectHandler("/dashboard/", http.StatusFound))
router.Handle("/dashboard", http.RedirectHandler("/dashboard/", http.StatusFound))
router.Handle("/dashboard/", vue.IndexFile())
router.Handle("/favicon.png", vue.ServeFaviconDashboard())
router.Handle("/favicon.ico", vue.ServeFaviconDashboard())
router.PathPrefix("/dashboard/").Handler(vue.IndexFileOnNotFound())
router.PathPrefix("/k8s/clusters/local").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
url := strings.TrimPrefix(req.URL.Path, "/k8s/clusters/local")
if url == "" {
Expand All @@ -44,5 +83,27 @@ func New(path string, offline string) http.Handler {
http.Redirect(rw, req, url, http.StatusFound)
})

router.PathPrefix("/v1/").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
url := strings.TrimPrefix(req.URL.Path, "/v1/")
if url == "" {
url = "/"
}
http.Redirect(rw, req, url, http.StatusFound)
})

router.PathPrefix("/v3/").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
url := strings.TrimPrefix(req.URL.Path, "/v3/settings/")
if url == "" {
url = "/"
}
http.Redirect(rw, req, url, http.StatusNotFound)
})

spa := spaHandler{staticPath: path, indexPath: "index.html"}
router.Handle("/dashboard", http.RedirectHandler("/dashboard/", http.StatusFound))
router.Handle("/dashboard/", vue.IndexFile())
router.PathPrefix("/").Handler(spa)
router.PathPrefix("/dashboard/").Handler(spa)

return router
}

0 comments on commit 6677656

Please sign in to comment.