From 1ef1a852fcb3c5178ade088a6658e4b712f5fbfe Mon Sep 17 00:00:00 2001 From: Valerio Ageno Date: Tue, 14 May 2024 20:42:32 +0200 Subject: [PATCH] feat: add support for static files --- crates/axum_bundler/src/bundle.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/axum_bundler/src/bundle.rs b/crates/axum_bundler/src/bundle.rs index a726a48f..1af5f3a5 100644 --- a/crates/axum_bundler/src/bundle.rs +++ b/crates/axum_bundler/src/bundle.rs @@ -5,19 +5,21 @@ use std::io::prelude::*; use std::path::Path; use std::path::PathBuf; -const AXUM_ENTRY_POINT: &'static str = r##"// File automatically generated +const AXUM_ENTRY_POINT: &'static str = r##" +// File automatically generated // Do not manually change it use axum::response::Html; -use axum::{routing::get, Router}; +use axum::{http::Uri, routing::get, Router}; use ssr_rs::Ssr; use std::cell::RefCell; use std::fs::read_to_string; +use tower_http::services::ServeDir; thread_local! { static SSR: RefCell> = RefCell::new( Ssr::from( - read_to_string("out/server/server-main.js").unwrap(), + read_to_string("./out/server/server-main.js").unwrap(), "" ).unwrap() ) @@ -29,20 +31,26 @@ async fn main() { let app = Router::new() // ROUTE_BUILDER - .route("/*key", get(catch_all)); + .fallback_service(ServeDir::new("public").fallback(get(catch_all))); - let app = app.fallback("Catch all route"); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); } -async fn catch_all() -> Html { - let result = SSR.with(|ssr| ssr.borrow_mut().render_to_string(None)); - Html(result.unwrap()) +async fn catch_all(uri: Uri) -> Html { + dbg!(&uri.path()); + let result = SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(&uri.path()))); + + match result { + Ok(html) => Html(html), + _ => Html("500 internal server error".to_string()), + } } + // ROUTE_DECLARATIONS + "##; fn create_main_file(base_path: &Path, bundled_file: &String) {