2023-06-04 20:11:02 +02:00
|
|
|
use cfg_if::cfg_if;
|
|
|
|
|
|
|
|
cfg_if! { if #[cfg(feature = "ssr")] {
|
|
|
|
use axum::{
|
|
|
|
body::{boxed, Body, BoxBody},
|
2023-10-20 23:06:21 +02:00
|
|
|
extract::State,
|
2023-06-04 20:11:02 +02:00
|
|
|
response::IntoResponse,
|
|
|
|
http::{Request, Response, StatusCode, Uri},
|
|
|
|
};
|
|
|
|
use axum::response::Response as AxumResponse;
|
|
|
|
use tower::ServiceExt;
|
|
|
|
use tower_http::services::ServeDir;
|
2023-10-20 23:06:21 +02:00
|
|
|
use leptos::{LeptosOptions, view};
|
|
|
|
use crate::app::App;
|
2023-06-04 20:11:02 +02:00
|
|
|
|
2023-10-20 23:06:21 +02:00
|
|
|
pub async fn file_and_error_handler(uri: Uri, State(options): State<LeptosOptions>, req: Request<Body>) -> AxumResponse {
|
2023-06-04 20:11:02 +02:00
|
|
|
let root = options.site_root.clone();
|
|
|
|
let res = get_static_file(uri.clone(), &root).await.unwrap();
|
|
|
|
|
|
|
|
if res.status() == StatusCode::OK {
|
2023-10-20 23:06:21 +02:00
|
|
|
res.into_response()
|
2023-06-04 20:11:02 +02:00
|
|
|
} else{
|
|
|
|
let handler = leptos_axum::render_app_to_stream(
|
|
|
|
options.to_owned(),
|
2023-10-20 23:06:21 +02:00
|
|
|
move || view!{ <App/> }
|
2023-06-04 20:11:02 +02:00
|
|
|
);
|
|
|
|
handler(req).await.into_response()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_static_file(uri: Uri, root: &str) -> Result<Response<BoxBody>, (StatusCode, String)> {
|
|
|
|
let req = Request::builder().uri(uri.clone()).body(Body::empty()).unwrap();
|
|
|
|
// `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
|
|
|
|
// This path is relative to the cargo root
|
|
|
|
match ServeDir::new(root).oneshot(req).await {
|
|
|
|
Ok(res) => Ok(res.map(boxed)),
|
|
|
|
Err(err) => Err((
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
format!("Something went wrong: {err}"),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|