From 3531ca64bb0d30d9a9df1b76ebda74cdef7591cd Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Wed, 21 Jun 2023 08:06:52 -0400 Subject: [PATCH] examples: update `leptos-tailwind-axum` to use main branch (#1218) --- examples/leptos-tailwind-axum/Cargo.toml | 10 ++- .../src/error_template.rs | 75 ------------------- .../src/{fileserv.rs => fallback.rs} | 22 +++--- examples/leptos-tailwind-axum/src/lib.rs | 3 +- examples/leptos-tailwind-axum/src/main.rs | 16 ++-- 5 files changed, 23 insertions(+), 103 deletions(-) delete mode 100644 examples/leptos-tailwind-axum/src/error_template.rs rename examples/leptos-tailwind-axum/src/{fileserv.rs => fallback.rs} (64%) diff --git a/examples/leptos-tailwind-axum/Cargo.toml b/examples/leptos-tailwind-axum/Cargo.toml index 254ae9d61..34edeb57b 100644 --- a/examples/leptos-tailwind-axum/Cargo.toml +++ b/examples/leptos-tailwind-axum/Cargo.toml @@ -11,10 +11,12 @@ axum = { version = "0.6.18", optional = true } console_error_panic_hook = "0.1.7" console_log = "1" cfg-if = "1" -leptos = { version = "0.3", default-features = false, features = ["serde"] } -leptos_axum = { version = "0.3", optional = true } -leptos_meta = { version = "0.3", default-features = false } -leptos_router = { version = "0.3", default-features = false } +leptos = { path = "../../leptos", default-features = false, features = [ + "serde", +] } +leptos_meta = { path = "../../meta", default-features = false } +leptos_axum = { path = "../../integrations/axum", default-features = false, optional = true } +leptos_router = { path = "../../router", default-features = false } log = "0.4.17" simple_logger = "4" tokio = { version = "1.28.1", optional = true } diff --git a/examples/leptos-tailwind-axum/src/error_template.rs b/examples/leptos-tailwind-axum/src/error_template.rs deleted file mode 100644 index 6d6a2a1cc..000000000 --- a/examples/leptos-tailwind-axum/src/error_template.rs +++ /dev/null @@ -1,75 +0,0 @@ -use cfg_if::cfg_if; -use http::status::StatusCode; -use leptos::*; -#[cfg(feature = "ssr")] -use leptos_axum::ResponseOptions; -use thiserror::Error; - -#[derive(Clone, Debug, Error)] -pub enum AppError { - #[error("Not Found")] - NotFound, -} - -impl AppError { - pub fn status_code(&self) -> StatusCode { - match self { - AppError::NotFound => StatusCode::NOT_FOUND, - } - } -} - -// A basic function to display errors served by the error boundaries. -// Feel free to do more complicated things here than just displaying the error. -#[component] -pub fn ErrorTemplate( - cx: Scope, - #[prop(optional)] outside_errors: Option, - #[prop(optional)] errors: Option>, -) -> impl IntoView { - let errors = match outside_errors { - Some(e) => create_rw_signal(cx, e), - None => match errors { - Some(e) => e, - None => panic!("No Errors found and we expected errors!"), - }, - }; - // Get Errors from Signal - let errors = errors.get(); - - // Downcast lets us take a type that implements `std::error::Error` - let errors: Vec = errors - .into_iter() - .filter_map(|(_k, v)| v.downcast_ref::().cloned()) - .collect(); - println!("Errors: {errors:#?}"); - - // Only the response code for the first error is actually sent from the server - // this may be customized by the specific application - cfg_if! { if #[cfg(feature="ssr")] { - let response = use_context::(cx); - if let Some(response) = response { - response.set_status(errors[0].status_code()); - } - }} - - view! {cx, -

{if errors.len() > 1 {"Errors"} else {"Error"}}

- {error_code.to_string()} -

"Error: " {error_string}

- } - } - /> - } -} diff --git a/examples/leptos-tailwind-axum/src/fileserv.rs b/examples/leptos-tailwind-axum/src/fallback.rs similarity index 64% rename from examples/leptos-tailwind-axum/src/fileserv.rs rename to examples/leptos-tailwind-axum/src/fallback.rs index e180bcb75..c4b83896a 100644 --- a/examples/leptos-tailwind-axum/src/fileserv.rs +++ b/examples/leptos-tailwind-axum/src/fallback.rs @@ -3,29 +3,27 @@ use cfg_if::cfg_if; cfg_if! { if #[cfg(feature = "ssr")] { use axum::{ body::{boxed, Body, BoxBody}, - extract::Extension, + extract::State, response::IntoResponse, http::{Request, Response, StatusCode, Uri}, }; use axum::response::Response as AxumResponse; use tower::ServiceExt; use tower_http::services::ServeDir; - use std::sync::Arc; - use leptos::*; - use crate::error_template::ErrorTemplate; - use crate::error_template::AppError; + use leptos::{LeptosOptions, view}; + use crate::app::App; - pub async fn file_and_error_handler(uri: Uri, Extension(options): Extension>, req: Request) -> AxumResponse { - let options = &*options; + 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(); if res.status() == StatusCode::OK { - res.into_response() - } else { - let mut errors = Errors::default(); - errors.insert_with_default_key(AppError::NotFound); - let handler = leptos_axum::render_app_to_stream(options.to_owned(), move |cx| view!{cx, }); + res.into_response() + } else{ + let handler = leptos_axum::render_app_to_stream( + options.to_owned(), + move |cx| view!{ cx, } + ); handler(req).await.into_response() } } diff --git a/examples/leptos-tailwind-axum/src/lib.rs b/examples/leptos-tailwind-axum/src/lib.rs index 7190a49f8..cfe2cd2b8 100644 --- a/examples/leptos-tailwind-axum/src/lib.rs +++ b/examples/leptos-tailwind-axum/src/lib.rs @@ -1,7 +1,6 @@ use cfg_if::cfg_if; pub mod app; -pub mod error_template; -pub mod fileserv; +pub mod fallback; cfg_if! { if #[cfg(feature = "hydrate")] { use leptos::*; diff --git a/examples/leptos-tailwind-axum/src/main.rs b/examples/leptos-tailwind-axum/src/main.rs index 8f9b1a742..f36c194ff 100644 --- a/examples/leptos-tailwind-axum/src/main.rs +++ b/examples/leptos-tailwind-axum/src/main.rs @@ -1,12 +1,11 @@ #[cfg(feature = "ssr")] #[tokio::main] async fn main() { - use axum::{extract::Extension, routing::post, Router}; + use axum::{routing::post, Router}; use leptos::*; use leptos_axum::{generate_route_list, LeptosRoutes}; - use leptos_tailwind::{app::*, fileserv::file_and_error_handler}; + use leptos_tailwind::{app::*, fallback::file_and_error_handler}; use log::info; - use std::sync::Arc; simple_logger::init_with_level(log::Level::Info) .expect("couldn't initialize logging"); @@ -17,20 +16,17 @@ async fn main() { // Alternately a file can be specified such as Some("Cargo.toml") // The file would need to be included with the executable when moved to deployment let conf = get_configuration(None).await.unwrap(); + let addr = conf.leptos_options.site_addr; let leptos_options = conf.leptos_options; - let addr = leptos_options.site_addr; + // Generate the list of routes in your Leptos App let routes = generate_route_list(|cx| view! { cx, }).await; // build our application with a route let app = Router::new() .route("/api/*fn_name", post(leptos_axum::handle_server_fns)) - .leptos_routes( - leptos_options.clone(), - routes, - |cx| view! { cx, }, - ) + .leptos_routes(&leptos_options, routes, |cx| view! { cx, }) .fallback(file_and_error_handler) - .layer(Extension(Arc::new(leptos_options))); + .with_state(leptos_options); // run our app with hyper // `axum::Server` is a re-export of `hyper::Server`