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.
3
10
4
11
use super :: prelude:: * ;
5
12
use std:: fmt:: Write ;
6
13
7
14
use crate :: util:: { errors:: NotFound , AppResponse , Error , RequestProxy } ;
8
15
9
16
use conduit:: { Body , HandlerResult } ;
17
+ use conduit_static:: Static ;
10
18
use reqwest:: blocking:: Client ;
11
19
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 ,
16
23
fastboot_client : Option < Client > ,
17
24
}
18
25
19
- impl Default for EmberIndexRewrite {
20
- fn default ( ) -> EmberIndexRewrite {
26
+ impl EmberHtml {
27
+ pub fn new ( path : & str ) -> Self {
21
28
let fastboot_client = match dotenv:: var ( "USE_FASTBOOT" ) {
22
29
Ok ( val) if val == "staging-experimental" => Some ( Client :: new ( ) ) ,
23
30
_ => None ,
24
31
} ;
25
32
26
- EmberIndexRewrite {
27
- handler : None ,
33
+ Self {
34
+ api_handler : None ,
35
+ static_handler : Static :: new ( path) ,
28
36
fastboot_client,
29
37
}
30
38
}
31
39
}
32
40
33
- impl AroundMiddleware for EmberIndexRewrite {
41
+ impl AroundMiddleware for EmberHtml {
34
42
fn with_handler ( & mut self , handler : Box < dyn Handler > ) {
35
- self . handler = Some ( handler) ;
43
+ self . api_handler = Some ( handler) ;
36
44
}
37
45
}
38
46
39
- impl Handler for EmberIndexRewrite {
47
+ impl Handler for EmberHtml {
40
48
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 ( ) ;
42
50
43
51
// The "/git/" prefix is only used in development (when within a docker container)
44
52
if req. path ( ) . starts_with ( "/api/" ) || req. path ( ) . starts_with ( "/git/" ) {
45
- handler . call ( req)
53
+ api_handler . call ( req)
46
54
} else {
47
55
if let Some ( client) = & self . fastboot_client {
48
56
// During local fastboot development, forward requests to the local fastboot server.
@@ -58,7 +66,8 @@ impl Handler for EmberIndexRewrite {
58
66
. any ( |val| val. to_str ( ) . unwrap_or_default ( ) . contains ( "html" ) )
59
67
{
60
68
// 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" ) )
62
71
} else {
63
72
// Return a 404 to crawlers that don't send `Accept: text/hml`.
64
73
// This is to preserve legacy behavior and will likely change.
0 commit comments