From 7bb35b2b09046bb0c7a96cefd2bf91477e8c0d9d Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Tue, 22 Nov 2022 10:21:15 +1100 Subject: [PATCH] Handle index.html correctly in custom served folders (#3168) * getStringMapString return nil if key not found * Refactor custom routes. Handle /index.html --- internal/api/routes_custom.go | 35 +++++++++++++++++++++++++++++++ internal/api/server.go | 15 +++---------- internal/manager/config/config.go | 10 ++++++++- 3 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 internal/api/routes_custom.go diff --git a/internal/api/routes_custom.go b/internal/api/routes_custom.go new file mode 100644 index 000000000..731bbc586 --- /dev/null +++ b/internal/api/routes_custom.go @@ -0,0 +1,35 @@ +package api + +import ( + "net/http" + "strings" + + "github.com/go-chi/chi" + "github.com/stashapp/stash/internal/manager/config" +) + +type customRoutes struct { + servedFolders config.URLMap +} + +func (rs customRoutes) Routes() chi.Router { + r := chi.NewRouter() + + r.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) { + r.URL.Path = strings.Replace(r.URL.Path, "/custom", "", 1) + + // http.FileServer redirects to / if the path ends with index.html + r.URL.Path = strings.TrimSuffix(r.URL.Path, "/index.html") + + // map the path to the applicable filesystem location + var dir string + r.URL.Path, dir = rs.servedFolders.GetFilesystemLocation(r.URL.Path) + if dir != "" { + http.FileServer(http.Dir(dir)).ServeHTTP(w, r) + } else { + http.NotFound(w, r) + } + }) + + return r +} diff --git a/internal/api/server.go b/internal/api/server.go index 62d50bb94..124c89739 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -231,18 +231,9 @@ func Start() error { // Serve static folders customServedFolders := c.GetCustomServedFolders() if customServedFolders != nil { - r.HandleFunc("/custom/*", func(w http.ResponseWriter, r *http.Request) { - r.URL.Path = strings.Replace(r.URL.Path, "/custom", "", 1) - - // map the path to the applicable filesystem location - var dir string - r.URL.Path, dir = customServedFolders.GetFilesystemLocation(r.URL.Path) - if dir != "" { - http.FileServer(http.Dir(dir)).ServeHTTP(w, r) - } else { - http.NotFound(w, r) - } - }) + r.Mount("/custom", customRoutes{ + servedFolders: customServedFolders, + }.Routes()) } customUILocation := c.GetCustomUILocation() diff --git a/internal/manager/config/config.go b/internal/manager/config/config.go index 73c116bc8..8c47b1989 100644 --- a/internal/manager/config/config.go +++ b/internal/manager/config/config.go @@ -466,7 +466,15 @@ func (i *Instance) getStringMapString(key string) map[string]string { i.RLock() defer i.RUnlock() - return i.viper(key).GetStringMapString(key) + ret := i.viper(key).GetStringMapString(key) + + // GetStringMapString returns an empty map regardless of whether the + // key exists or not. + if len(ret) == 0 { + return nil + } + + return ret } type StashConfig struct {