From df5c874a333e38ade9c033a00e97ad046eacad32 Mon Sep 17 00:00:00 2001 From: Daniel Boros Date: Tue, 3 Sep 2024 22:01:27 +0200 Subject: [PATCH 1/3] chore: remove unneccesray files --- src/fileserv.rs | 60 ------------------------------------------------- src/lib.rs | 2 -- src/main.rs | 6 ++--- 3 files changed, 3 insertions(+), 65 deletions(-) delete mode 100644 src/fileserv.rs diff --git a/src/fileserv.rs b/src/fileserv.rs deleted file mode 100644 index a499201..0000000 --- a/src/fileserv.rs +++ /dev/null @@ -1,60 +0,0 @@ -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, - req: Request, -) -> 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, - 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/lib.rs b/src/lib.rs index c6edca8..eb725e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,6 @@ pub mod app; pub mod context; pub mod exercises; -#[cfg(feature = "ssr")] -pub mod fileserv; pub mod instruction; #[cfg(feature = "ssr")] pub mod redirect; diff --git a/src/main.rs b/src/main.rs index ff50001..5c96ce1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #[cfg(feature = "ssr")] #[tokio::main] async fn main() { + use axum::http::StatusCode; use axum::Router; use chrono::{Duration, Utc}; use leptos::*; @@ -8,8 +9,7 @@ async fn main() { use std::fs; use tokio_cron_scheduler::{Job, JobScheduler}; use tower_http::trace::TraceLayer; - use tryrust::app::*; - use tryrust::fileserv::file_and_error_handler; + use tryrust::app::App; use tryrust::redirect::redirect_www; tracing_subscriber::fmt() @@ -63,7 +63,7 @@ async fn main() { // build our application with a route let app = Router::new() .leptos_routes(&leptos_options, routes, App) - .fallback(file_and_error_handler) + .fallback((|| (StatusCode::NOT_FOUND, "Not Found"))()) .layer( tower::ServiceBuilder::new() .layer(TraceLayer::new_for_http()) From 442fce17b1e96db1b84905b5593677fee3dd4be9 Mon Sep 17 00:00:00 2001 From: Daniel Boros Date: Tue, 3 Sep 2024 22:02:01 +0200 Subject: [PATCH 2/3] chore: add comment --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index 5c96ce1..c88c492 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,6 +63,7 @@ async fn main() { // build our application with a route let app = Router::new() .leptos_routes(&leptos_options, routes, App) + // TODO: new version in v.0.7.0 .fallback((|| (StatusCode::NOT_FOUND, "Not Found"))()) .layer( tower::ServiceBuilder::new() From 6f620fe34d0b687f6e2a69d78b5cc24ae6a56ec4 Mon Sep 17 00:00:00 2001 From: Daniel Boros Date: Tue, 3 Sep 2024 22:46:55 +0200 Subject: [PATCH 3/3] fix: restore fileserv2 --- src/fileserv.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ src/main.rs | 3 ++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/fileserv.rs diff --git a/src/fileserv.rs b/src/fileserv.rs new file mode 100644 index 0000000..a499201 --- /dev/null +++ b/src/fileserv.rs @@ -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, + req: Request, +) -> 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, + 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/lib.rs b/src/lib.rs index eb725e3..c6edca8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,8 @@ pub mod app; pub mod context; pub mod exercises; +#[cfg(feature = "ssr")] +pub mod fileserv; pub mod instruction; #[cfg(feature = "ssr")] pub mod redirect; diff --git a/src/main.rs b/src/main.rs index c88c492..26075cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ async fn main() { use chrono::{Duration, Utc}; use leptos::*; use leptos_axum::{generate_route_list, LeptosRoutes}; + use tryrust::fileserv::file_and_error_handler; use std::fs; use tokio_cron_scheduler::{Job, JobScheduler}; use tower_http::trace::TraceLayer; @@ -64,7 +65,7 @@ async fn main() { let app = Router::new() .leptos_routes(&leptos_options, routes, App) // TODO: new version in v.0.7.0 - .fallback((|| (StatusCode::NOT_FOUND, "Not Found"))()) + .fallback(file_and_error_handler) .layer( tower::ServiceBuilder::new() .layer(TraceLayer::new_for_http())