From dd35ad43caab407cc70ef3b153b3f94d57242ed9 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Thu, 21 Jun 2018 14:31:51 -0400 Subject: 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. --- app/server.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'app/server.go') diff --git a/app/server.go b/app/server.go index 7d229201d..d71a884d2 100644 --- a/app/server.go +++ b/app/server.go @@ -29,10 +29,17 @@ import ( type Server struct { Store store.Store WebSocketRouter *WebSocketRouter - Router *mux.Router - Server *http.Server - ListenAddr *net.TCPAddr - RateLimiter *RateLimiter + + // RootRouter is the starting point for all HTTP requests to the server. + RootRouter *mux.Router + + // Router is the starting point for all web, api4 and ws requests to the server. It differs + // from RootRouter only if the SiteURL contains a /subpath. + Router *mux.Router + + Server *http.Server + ListenAddr *net.TCPAddr + RateLimiter *RateLimiter didFinishListen chan struct{} } @@ -99,7 +106,7 @@ func redirectHTTPToHTTPS(w http.ResponseWriter, r *http.Request) { func (a *App) StartServer() error { mlog.Info("Starting Server...") - var handler http.Handler = &CorsWrapper{a.Config, a.Srv.Router} + var handler http.Handler = &CorsWrapper{a.Config, a.Srv.RootRouter} if *a.Config().RateLimitSettings.Enable { mlog.Info("RateLimiter is enabled") -- cgit v1.2.3-1-g7c22