Skip to content

Commit 064951e

Browse files
committed
Improve code and docs for serving Ember.js HTML
1 parent fe4e0a2 commit 064951e

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

src/middleware.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod prelude {
66
use self::app::AppMiddleware;
77
use self::current_user::CaptureUserIdFromCookie;
88
use self::debug::*;
9-
use self::ember_index_rewrite::EmberIndexRewrite;
9+
use self::ember_html::EmberHtml;
1010
use self::head::Head;
1111
use self::log_connection_pool_status::LogConnectionPoolStatus;
1212
use self::static_or_continue::StaticOrContinue;
@@ -15,7 +15,7 @@ pub mod app;
1515
mod block_traffic;
1616
pub mod current_user;
1717
mod debug;
18-
mod ember_index_rewrite;
18+
mod ember_html;
1919
mod ensure_well_formed_500;
2020
mod head;
2121
mod log_connection_pool_status;
@@ -72,13 +72,13 @@ pub fn build_middleware(app: Arc<App>, endpoints: R404) -> MiddlewareBuilder {
7272
// Parse and save the user_id from the session cookie as part of the authentication logic
7373
m.add(CaptureUserIdFromCookie);
7474

75+
// Note: The following `m.around()` middleware is run from bottom to top
76+
7577
// Serve the static files in the *dist* directory, which are the frontend assets.
7678
// Not needed for the backend tests.
7779
if env != Env::Test {
80+
m.around(EmberHtml::new("dist"));
7881
m.around(StaticOrContinue::new("dist"));
79-
m.around(EmberIndexRewrite::default());
80-
m.around(StaticOrContinue::new("dist"));
81-
// Note: around middleware is run from bottom to top, so the rewrite occurs first
8282
}
8383

8484
m.around(Head::default());

src/middleware/ember_index_rewrite.rs renamed to src/middleware/ember_html.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,56 @@
1-
//! Rewrite the request path to "index.html" if the path doesn't start
2-
//! with "/api" and the Accept header contains "html".
1+
//! Serve the Ember.js frontend HTML
2+
//!
3+
//! Paths intended for the inner `api_handler` are passed along to the remaining middleware layers
4+
//! as normal. Requests not intended for the backend will be served HTML to boot the Ember.js
5+
//! frontend. During local development, if so configured, these requests will instead be proxied to
6+
//! Ember FastBoot (`node ./fastboot.js`).
7+
//!
8+
//! For now, there is an additional check to see if the `Accept` header contains "html". This is
9+
//! likely to be removed in the future.
310
411
use super::prelude::*;
512
use std::fmt::Write;
613

714
use crate::util::{errors::NotFound, AppResponse, Error, RequestProxy};
815

916
use conduit::{Body, HandlerResult};
17+
use conduit_static::Static;
1018
use reqwest::blocking::Client;
1119

12-
// Can't derive debug because of Handler and Static.
13-
#[allow(missing_debug_implementations)]
14-
pub struct EmberIndexRewrite {
15-
handler: Option<Box<dyn Handler>>,
20+
pub(super) struct EmberHtml {
21+
api_handler: Option<Box<dyn Handler>>,
22+
static_handler: Static,
1623
fastboot_client: Option<Client>,
1724
}
1825

19-
impl Default for EmberIndexRewrite {
20-
fn default() -> EmberIndexRewrite {
26+
impl EmberHtml {
27+
pub fn new(path: &str) -> Self {
2128
let fastboot_client = match dotenv::var("USE_FASTBOOT") {
2229
Ok(val) if val == "staging-experimental" => Some(Client::new()),
2330
_ => None,
2431
};
2532

26-
EmberIndexRewrite {
27-
handler: None,
33+
Self {
34+
api_handler: None,
35+
static_handler: Static::new(path),
2836
fastboot_client,
2937
}
3038
}
3139
}
3240

33-
impl AroundMiddleware for EmberIndexRewrite {
41+
impl AroundMiddleware for EmberHtml {
3442
fn with_handler(&mut self, handler: Box<dyn Handler>) {
35-
self.handler = Some(handler);
43+
self.api_handler = Some(handler);
3644
}
3745
}
3846

39-
impl Handler for EmberIndexRewrite {
47+
impl Handler for EmberHtml {
4048
fn call(&self, req: &mut dyn RequestExt) -> HandlerResult {
41-
let handler = self.handler.as_ref().unwrap();
49+
let api_handler = self.api_handler.as_ref().unwrap();
4250

4351
// The "/git/" prefix is only used in development (when within a docker container)
4452
if req.path().starts_with("/api/") || req.path().starts_with("/git/") {
45-
handler.call(req)
53+
api_handler.call(req)
4654
} else {
4755
if let Some(client) = &self.fastboot_client {
4856
// During local fastboot development, forward requests to the local fastboot server.
@@ -58,7 +66,8 @@ impl Handler for EmberIndexRewrite {
5866
.any(|val| val.to_str().unwrap_or_default().contains("html"))
5967
{
6068
// Serve static Ember page to bootstrap the frontend
61-
handler.call(&mut RequestProxy::rewrite_path(req, "/index.html"))
69+
self.static_handler
70+
.call(&mut RequestProxy::rewrite_path(req, "/index.html"))
6271
} else {
6372
// Return a 404 to crawlers that don't send `Accept: text/hml`.
6473
// This is to preserve legacy behavior and will likely change.

0 commit comments

Comments
 (0)