From e7686d4919a80f21ca3e17dbaef0b4ef22e61ed3 Mon Sep 17 00:00:00 2001 From: rek-malorm <70321198+Rek-Malorm@users.noreply.github.com> Date: Sat, 17 Aug 2024 12:58:38 +1200 Subject: [PATCH 1/3] Update template to use newer dependencies --- Cargo.toml | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f135f57..705885d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,25 +7,25 @@ edition = "2021" crate-type = ["cdylib", "rlib"] [dependencies] -axum = { version = "0.6.20", optional = true } +axum = { version = "0.7", optional = true } console_error_panic_hook = "0.1" console_log = "1" cfg-if = "1" -leptos = { version = "0.5.4", features = ["nightly"] } -leptos_axum = { version = "0.5.4", optional = true } -leptos_meta = { version = "0.5.4", features = ["nightly"] } -leptos_router = { version = "0.5.4", features = ["nightly"] } +leptos = { version = "0.6", features = ["nightly"] } +leptos_axum = { version = "0.6", optional = true } +leptos_meta = { version = "0.6", features = ["nightly"] } +leptos_router = { version = "0.6", features = ["nightly"] } log = "0.4" -simple_logger = "4" -tokio = { version = "1.25.0", optional = true } -tower = { version = "0.4.13", optional = true } -tower-http = { version = "0.4", features = ["fs"], optional = true } -wasm-bindgen = "=0.2.89" -thiserror = "1.0.38" +simple_logger = "5" +tokio = { version = "1", optional = true } +tower = { version = "0.4", optional = true } +tower-http = { version = "0.5", features = ["fs"], optional = true } +wasm-bindgen = "=0.2.92" +thiserror = "1" tracing = { version = "0.1.37", optional = true } -http = "0.2.8" -axum-aws-lambda = { version = "0.5.0", optional = true } -lambda_http = { version = "0.8", optional = true } +http = "1" +axum-aws-lambda = { version = "0.7", optional = true } +lambda_http = { version = "0.13", optional = true } [features] hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"] From e0895356eb2513159869d695e71af5e9b370619e Mon Sep 17 00:00:00 2001 From: rek-malorm <70321198+Rek-Malorm@users.noreply.github.com> Date: Sat, 17 Aug 2024 14:30:30 +1200 Subject: [PATCH 2/3] Update implementation to support newer dependencies --- Cargo.toml | 8 +++--- src/fileserv.rs | 66 +++++++++++++++++++++++++++++++++---------------- src/main.rs | 6 ++--- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 705885d..342bad8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,14 +17,14 @@ leptos_meta = { version = "0.6", features = ["nightly"] } leptos_router = { version = "0.6", features = ["nightly"] } log = "0.4" simple_logger = "5" -tokio = { version = "1", optional = true } -tower = { version = "0.4", optional = true } +tokio = { version = "1", features = ["rt-multi-thread"], optional = true } +tower = { version = "0.5", features = ["util"], optional = true } tower-http = { version = "0.5", features = ["fs"], optional = true } -wasm-bindgen = "=0.2.92" +wasm-bindgen = "=0.2.93" thiserror = "1" tracing = { version = "0.1.37", optional = true } http = "1" -axum-aws-lambda = { version = "0.7", optional = true } +axum-aws-lambda = { version = "0.8", optional = true } lambda_http = { version = "0.13", optional = true } [features] diff --git a/src/fileserv.rs b/src/fileserv.rs index acc1635..e8e5fde 100644 --- a/src/fileserv.rs +++ b/src/fileserv.rs @@ -5,7 +5,7 @@ cfg_if! { if #[cfg(feature = "ssr")] { body::{boxed, Body, BoxBody}, extract::State, response::IntoResponse, - http::{Request, Response, StatusCode, Uri}, + http::{Request, Response, StatusCode} }; use axum::response::Response as AxumResponse; use tower::ServiceExt; @@ -13,28 +13,52 @@ cfg_if! { if #[cfg(feature = "ssr")] { use leptos::*; use crate::app::App; - pub async fn file_and_error_handler(uri: Uri, State(options): State, req: Request) -> AxumResponse { - let root = options.site_root.clone(); - let res = get_static_file(uri.clone(), &root).await.unwrap(); +pub async fn file_and_error_handler( + State(options): State, + req: Request, +) -> AxumResponse { + let root = options.site_root.clone(); + let (parts, body) = req.into_parts(); - if res.status() == StatusCode::OK { - res.into_response() - } else { - let handler = leptos_axum::render_app_to_stream(options.to_owned(), move || view!{}); - handler(req).await.into_response() - } + 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()); } - async fn get_static_file(uri: Uri, root: &str) -> Result, (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}"), - )), - } + 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() + } +} + +pub async fn get_static_file( + request: Request, + root: &str, +) -> Result, (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}"), + )), } +} }} diff --git a/src/main.rs b/src/main.rs index e271e07..3161029 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,9 +30,9 @@ async fn main() { // In development, we use the Hyper server #[cfg(debug_assertions)] { - log::info!("listening on http://{}", &addr); - axum::Server::bind(&addr) - .serve(app.into_make_service()) + logging::log!("listening on http://{}", &addr); + let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); + axum::serve(listener, app.into_make_service()) .await .unwrap(); } From 2504c1435b46d5c8e7b48941a52a5ac15f1385bb Mon Sep 17 00:00:00 2001 From: rek-malorm <70321198+Rek-Malorm@users.noreply.github.com> Date: Sat, 17 Aug 2024 14:33:50 +1200 Subject: [PATCH 3/3] Connect axum import --- src/fileserv.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fileserv.rs b/src/fileserv.rs index e8e5fde..0f39803 100644 --- a/src/fileserv.rs +++ b/src/fileserv.rs @@ -2,7 +2,7 @@ use cfg_if::cfg_if; cfg_if! { if #[cfg(feature = "ssr")] { use axum::{ - body::{boxed, Body, BoxBody}, + body::Body, extract::State, response::IntoResponse, http::{Request, Response, StatusCode}