From 71095c4367d0bef8fcb4c5cb9c10cc323deb7908 Mon Sep 17 00:00:00 2001 From: Justin Geibel Date: Thu, 18 Jul 2019 00:25:17 -0400 Subject: [PATCH] Serve Ember bootstrap HTML for all non-api requests The backend no longer checks for an "html" in the `Accept` header. With the exception of 3 session related routes, all paths not starting with "/api" will be redirected to the static Ember bootstrap page. As a result of this change all non-api requests that don't contain "html" in the `Accept` header will now unconditionally return `200`, rather than `404`. In a sense, this expands the scope of #556 to all requests, not just those that set the header. It also inverts the problem described in #788, effectively turning it into a duplicate of #556. Fixes: #163 --- src/middleware/ember_index_rewrite.rs | 25 ++++++++++++------------- src/router.rs | 2 ++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/middleware/ember_index_rewrite.rs b/src/middleware/ember_index_rewrite.rs index 0b6117ce81d..8e0478586b8 100644 --- a/src/middleware/ember_index_rewrite.rs +++ b/src/middleware/ember_index_rewrite.rs @@ -25,21 +25,20 @@ impl AroundMiddleware for EmberIndexRewrite { impl Handler for EmberIndexRewrite { fn call(&self, req: &mut dyn Request) -> Result> { - // If the client is requesting html, then we've only got one page so - // rewrite the request. - let wants_html = req - .headers() - .find("Accept") - .map(|accept| accept.iter().any(|s| s.contains("html"))) - .unwrap_or(false); - // If the route starts with /api, just assume they want the API - // response and fall through. - let is_api_path = req.path().starts_with("/api"); let handler = self.handler.as_ref().unwrap(); - if wants_html && !is_api_path { - handler.call(&mut RequestProxy::rewrite_path(req, "/index.html")) - } else { + let is_backend_path = match req.path() { + // Special case routes used for authentication + "/authorize" | "/authorize_url" | "/logout" => true, + // Paths starting with `/api` are intended for the backend + path if path.starts_with("/api") => true, + _ => false, + }; + + if is_backend_path { handler.call(req) + } else { + // Serve static Ember page to bootstrap the frontend + handler.call(&mut RequestProxy::rewrite_path(req, "/index.html")) } } } diff --git a/src/router.rs b/src/router.rs index 6c653f4825f..e335d138bb9 100644 --- a/src/router.rs +++ b/src/router.rs @@ -108,6 +108,8 @@ pub fn build_router(app: &App) -> R404 { router.head("/api/v1/*path", R(Arc::clone(&api_router))); router.delete("/api/v1/*path", R(api_router)); + // These routes are special cased in the EmberIndexRewrite middleware. + // Avoid adding new ones and keep in sync with the list there! router.get("/authorize_url", C(user::session::github_authorize)); router.get("/authorize", C(user::session::github_access_token)); router.delete("/logout", C(user::session::logout));