-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::app::App; | ||
use axum::response::Response as AxumResponse; | ||
use axum::{ | ||
body::Body, | ||
extract::State, | ||
http::{Request, Response, StatusCode}, | ||
response::IntoResponse, | ||
}; | ||
use leptos::*; | ||
use tower::ServiceExt; | ||
use tower_http::services::ServeDir; | ||
|
||
pub async fn file_and_error_handler( | ||
State(options): State<LeptosOptions>, | ||
req: Request<Body>, | ||
) -> AxumResponse { | ||
let root = options.site_root.clone(); | ||
let (parts, body) = req.into_parts(); | ||
|
||
let mut static_parts = parts.clone(); | ||
static_parts.headers.clear(); | ||
if let Some(encodings) = parts.headers.get("accept-encoding") { | ||
static_parts | ||
.headers | ||
.insert("accept-encoding", encodings.clone()); | ||
} | ||
|
||
let res = get_static_file(Request::from_parts(static_parts, Body::empty()), &root) | ||
.await | ||
.unwrap(); | ||
|
||
if res.status() == StatusCode::OK { | ||
res.into_response() | ||
} else { | ||
let handler = leptos_axum::render_app_to_stream(options.to_owned(), App); | ||
handler(Request::from_parts(parts, body)) | ||
.await | ||
.into_response() | ||
} | ||
} | ||
|
||
async fn get_static_file( | ||
request: Request<Body>, | ||
root: &str, | ||
) -> Result<Response<Body>, (StatusCode, String)> { | ||
// `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) | ||
.precompressed_gzip() | ||
.precompressed_br() | ||
.oneshot(request) | ||
.await | ||
{ | ||
Ok(res) => Ok(res.into_response()), | ||
Err(err) => Err(( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
format!("Error serving files: {err}"), | ||
)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters