-
Notifications
You must be signed in to change notification settings - Fork 602
/
ember_html.rs
42 lines (39 loc) · 1.68 KB
/
ember_html.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! Serve the Ember.js frontend HTML
//!
//! Paths intended for the inner `api_handler` are passed along to the remaining middleware layers
//! as normal. Requests not intended for the backend will be served HTML to boot the Ember.js
//! frontend.
//!
//! For now, there is an additional check to see if the `Accept` header contains "html". This is
//! likely to be removed in the future.
use axum::extract::Request;
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use http::{header, StatusCode};
use tower::ServiceExt;
use tower_http::services::ServeFile;
pub async fn serve_html(request: Request, next: Next) -> Response {
let path = &request.uri().path();
// The "/git/" prefix is only used in development (when within a docker container)
if path.starts_with("/api/") || path.starts_with("/git/") {
next.run(request).await
} else if request
.headers()
.get_all(header::ACCEPT)
.iter()
.any(|val| val.to_str().unwrap_or_default().contains("html"))
{
// Serve static Ember page to bootstrap the frontend
ServeFile::new("dist/index.html")
.oneshot(request)
.await
.map(|response| response.map(axum::body::Body::new))
.unwrap_or_else(|_| StatusCode::INTERNAL_SERVER_ERROR.into_response())
} else {
// Return a 404 to crawlers that don't send `Accept: text/hml`.
// This is to preserve legacy behavior and will likely change.
// Most of these crawlers probably won't execute our frontend JS anyway, but
// it would be nice to bootstrap the app for crawlers that do execute JS.
StatusCode::NOT_FOUND.into_response()
}
}