diff options
author | Jesse Hallam <jesse.hallam@gmail.com> | 2018-06-21 14:31:51 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2018-06-21 11:31:51 -0700 |
commit | dd35ad43caab407cc70ef3b153b3f94d57242ed9 (patch) | |
tree | 67afe0fb6ad784e740d8b2a99d5de5c04342661c /web/static.go | |
parent | 46f969e5ddbe4404dbc82dbe78ab2fa101d9922e (diff) | |
download | chat-dd35ad43caab407cc70ef3b153b3f94d57242ed9.tar.gz chat-dd35ad43caab407cc70ef3b153b3f94d57242ed9.tar.bz2 chat-dd35ad43caab407cc70ef3b153b3f94d57242ed9.zip |
MM-10370: serve subpath (#8968)
* factor out GetSubpathFromConfig
* mv web/subpath.go to utils/subpath.go
* serve up web, api and ws on /subpath if configured
* pass config to utils.RenderWeb(App)?Error
This allows the methods to extract the configured subpath and redirect
to the appropriate `/subpath/error` handler.
* ensure GetSubpathFromConfig returns trailing slashes deterministically
* fix error 404 handling
* redirect /subpath to /subpath/
This is necessary for the static handler to match, otherwise none of the
registered routes find anything. This also makes it no longer necessary
to add trailing slashes in the root router.
Diffstat (limited to 'web/static.go')
-rw-r--r-- | web/static.go | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/web/static.go b/web/static.go index 1f76b2725..08da9e95e 100644 --- a/web/static.go +++ b/web/static.go @@ -6,6 +6,7 @@ package web import ( "fmt" "net/http" + "path" "path/filepath" "strings" @@ -18,13 +19,15 @@ import ( func (w *Web) InitStatic() { if *w.App.Config().ServiceSettings.WebserverMode != "disabled" { - UpdateAssetsSubpathFromConfig(w.App.Config()) + utils.UpdateAssetsSubpathFromConfig(w.App.Config()) staticDir, _ := utils.FindDir(model.CLIENT_DIR) mlog.Debug(fmt.Sprintf("Using client directory at %v", staticDir)) - staticHandler := staticHandler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir)))) - pluginHandler := pluginHandler(w.App.Config, http.StripPrefix("/static/plugins/", http.FileServer(http.Dir(*w.App.Config().PluginSettings.ClientDirectory)))) + subpath, _ := utils.GetSubpathFromConfig(w.App.Config()) + + staticHandler := staticHandler(http.StripPrefix(path.Join(subpath, "static"), http.FileServer(http.Dir(staticDir)))) + pluginHandler := pluginHandler(w.App.Config, http.StripPrefix(path.Join(subpath, "plugins"), http.FileServer(http.Dir(*w.App.Config().PluginSettings.ClientDirectory)))) if *w.App.Config().ServiceSettings.WebserverMode == "gzip" { staticHandler = gziphandler.GzipHandler(staticHandler) @@ -34,6 +37,13 @@ func (w *Web) InitStatic() { w.MainRouter.PathPrefix("/static/plugins/").Handler(pluginHandler) w.MainRouter.PathPrefix("/static/").Handler(staticHandler) w.MainRouter.Handle("/{anything:.*}", w.NewStaticHandler(root)).Methods("GET") + + // When a subpath is defined, it's necessary to handle redirects without a + // trailing slash. We don't want to use StrictSlash on the w.MainRouter and affect + // all routes, just /subpath -> /subpath/. + w.MainRouter.HandleFunc("", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, r.URL.String()+"/", http.StatusFound) + })) } } @@ -48,7 +58,7 @@ func root(c *Context, w http.ResponseWriter, r *http.Request) { return } - if IsApiCall(r) { + if IsApiCall(c.App, r) { Handle404(c.App, w, r) return } |