From 442dc1e041b898b2afc7f4363fe6de356e6015e4 Mon Sep 17 00:00:00 2001 From: Ben Wishovich Date: Fri, 30 Dec 2022 16:17:17 -0800 Subject: [PATCH] More changes to the examples --- examples/hackernews-axum/Cargo.toml | 10 ++++++++-- examples/hackernews-axum/src/lib.rs | 3 ++- examples/hackernews/Cargo.toml | 12 +++++++++--- examples/hackernews/src/lib.rs | 3 ++- examples/hackernews/src/main.rs | 7 ++++++- examples/todo-app-sqlite-axum/Cargo.toml | 8 +++++++- examples/todo-app-sqlite-axum/src/main.rs | 21 +++++++++++++++------ examples/todo-app-sqlite-axum/src/todo.rs | 2 +- examples/todo-app-sqlite/Cargo.toml | 3 +++ examples/todo-app-sqlite/src/main.rs | 1 - examples/todo-app-sqlite/src/todo.rs | 5 +++-- integrations/actix/src/lib.rs | 7 ++++--- integrations/axum/src/lib.rs | 7 ++++--- 13 files changed, 64 insertions(+), 25 deletions(-) diff --git a/examples/hackernews-axum/Cargo.toml b/examples/hackernews-axum/Cargo.toml index 9777e1faa..03f773a6a 100644 --- a/examples/hackernews-axum/Cargo.toml +++ b/examples/hackernews-axum/Cargo.toml @@ -53,11 +53,17 @@ ssr = [ denylist = ["axum", "tower", "tower-http", "tokio", "http", "leptos_axum"] skip_feature_sets = [["csr", "ssr"], ["csr", "hydrate"], ["ssr", "hydrate"]] -[package.metadata.leptos] +[[workspace.metadata.leptos]] +# The name of your leptos package +name = "leptos_hackernews_axum" +# The name of the leptos server/bin package. Can be different from your lib package if they are in different crates. Otherwise, they'd be the same +bin-package = "leptos_hackernews_axum" +# The name of the leptos frontend/lib package. Can be different from your bin package if they are in different crates. Otherwise, they'd be the same +lib-package = "leptos_hackernews_axum" # The name used by wasm-bindgen/cargo-leptos for the JS/WASM bundle. Defaults to the crate name output-name = "leptos_hackernews_axum" # The site root folder is where cargo-leptos generate all output. WARNING: all content of this folder will be erased on a rebuild. Use it in your server setup. -site-root = "/pkg" +site-root = "site/target" # The site-root relative folder where all compiled output (JS, WASM and CSS) is written # Defaults to pkg site-pkg-dir = "pkg" diff --git a/examples/hackernews-axum/src/lib.rs b/examples/hackernews-axum/src/lib.rs index d7f38a671..7c4e887bc 100644 --- a/examples/hackernews-axum/src/lib.rs +++ b/examples/hackernews-axum/src/lib.rs @@ -1,5 +1,5 @@ use cfg_if::cfg_if; -use leptos::{component, Scope, IntoView, provide_context, view}; +use leptos::{component, provide_context, view, IntoView, Scope}; use leptos_meta::*; use leptos_router::*; mod api; @@ -12,6 +12,7 @@ use routes::users::*; #[component] pub fn App(cx: Scope) -> impl IntoView { + provide_context(cx, MetaContext::default()); view! { cx, <> diff --git a/examples/hackernews/Cargo.toml b/examples/hackernews/Cargo.toml index a6fcf8bd7..eaf854873 100644 --- a/examples/hackernews/Cargo.toml +++ b/examples/hackernews/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "leptos-hackernews" +name = "leptos_hackernews" version = "0.1.0" edition = "2021" @@ -45,11 +45,17 @@ ssr = [ denylist = ["actix-files", "actix-web", "leptos_actix"] skip_feature_sets = [["csr", "ssr"], ["csr", "hydrate"], ["ssr", "hydrate"]] -[package.metadata.leptos] +[[workspace.metadata.leptos]] +# The name of your leptos package +name = "leptos_hackernews" +# The name of the leptos server/bin package. Can be different from your lib package if they are in different crates. Otherwise, they'd be the same +bin-package = "leptos_hackernews" +# The name of the leptos frontend/lib package. Can be different from your bin package if they are in different crates. Otherwise, they'd be the same +lib-package = "leptos_hackernews" # The name used by wasm-bindgen/cargo-leptos for the JS/WASM bundle. Defaults to the crate name output-name = "leptos_hackernews" # The site root folder is where cargo-leptos generate all output. WARNING: all content of this folder will be erased on a rebuild. Use it in your server setup. -site-root = "/pkg" +site-root = "pkg" # The site-root relative folder where all compiled output (JS, WASM and CSS) is written # Defaults to pkg site-pkg-dir = "pkg" diff --git a/examples/hackernews/src/lib.rs b/examples/hackernews/src/lib.rs index 54db0ef20..55aa0863d 100644 --- a/examples/hackernews/src/lib.rs +++ b/examples/hackernews/src/lib.rs @@ -1,5 +1,5 @@ use cfg_if::cfg_if; -use leptos::{component, Scope, IntoView, provide_context, view}; +use leptos::{component, provide_context, view, IntoView, Scope}; use leptos_meta::*; use leptos_router::*; mod api; @@ -11,6 +11,7 @@ use routes::users::*; #[component] pub fn App(cx: Scope) -> impl IntoView { + provide_context(cx, MetaContext::default()); view! { cx, <> diff --git a/examples/hackernews/src/main.rs b/examples/hackernews/src/main.rs index 6c0e7468c..4f3a69920 100644 --- a/examples/hackernews/src/main.rs +++ b/examples/hackernews/src/main.rs @@ -20,8 +20,13 @@ cfg_if! { let addr = conf.leptos_options.site_address.clone(); HttpServer::new(move || { let leptos_options = &conf.leptos_options; + let site_root = &leptos_options.site_root; + let pkg_dir = &leptos_options.site_pkg_dir; + let bundle_path = format!("/{site_root}/{pkg_dir}"); + App::new() - .service(Files::new("/pkg", "./pkg")) + .service(Files::new("/pkg", "./pkg")) // used by wasm-pack and cargo run. Can be removed if using cargo-leptos + .service(Files::new(&bundle_path, format!("./{bundle_path}"))) // used by cargo-leptos. Can be removed if using wasm-pack and cargo run. .service(css) .route("/api/{tail:.*}", leptos_actix::handle_server_fns()) .route("/{tail:.*}", leptos_actix::render_app_to_stream(leptos_options.to_owned(), |cx| view! { cx, })) diff --git a/examples/todo-app-sqlite-axum/Cargo.toml b/examples/todo-app-sqlite-axum/Cargo.toml index 6039568ea..e4bc4f105 100644 --- a/examples/todo-app-sqlite-axum/Cargo.toml +++ b/examples/todo-app-sqlite-axum/Cargo.toml @@ -61,7 +61,13 @@ denylist = [ ] skip_feature_sets = [["csr", "ssr"], ["csr", "hydrate"], ["ssr", "hydrate"]] -[package.metadata.leptos] +[[workspace.metadata.leptos]] +# The name of your app +name = "todo_app_sqlite_axum" +# The name of the leptos server/bin package. Can be different from your lib package if they are in different crates. Otherwise, can be the same +bin-package = "todo_app_sqlite_axum" +# The name of the leptos frontend/lib package. Can be different from your bin package if they are in different crates. Otherwise, they'd be the same +lib-package = "todo_app_sqlite_axum" # The name used by wasm-bindgen/cargo-leptos for the JS/WASM bundle. Defaults to the crate name output-name = "todo_app_sqlite_axum" # The site root folder is where cargo-leptos generate all output. WARNING: all content of this folder will be erased on a rebuild. Use it in your server setup. diff --git a/examples/todo-app-sqlite-axum/src/main.rs b/examples/todo-app-sqlite-axum/src/main.rs index 6dd93e3e1..cc5e058ab 100644 --- a/examples/todo-app-sqlite-axum/src/main.rs +++ b/examples/todo-app-sqlite-axum/src/main.rs @@ -26,11 +26,25 @@ if #[cfg(feature = "ssr")] { crate::todo::register_server_functions(); + let conf = get_configuration(Some("Cargo.toml")).await.unwrap(); + let leptos_options = conf.leptos_options; + let site_root = &leptos_options.site_root; + let pkg_dir = &leptos_options.site_pkg_dir; + + // The URL path of the generated JS/WASM bundle from cargo-leptos + let bundle_path = format!("/{site_root}/{pkg_dir}"); + // The filesystem path of the generated JS/WASM bundle from cargo-leptos + let bundle_filepath = format!("./{site_root}/{pkg_dir}"); + let addr = leptos_options.site_address.clone(); + log::debug!("serving at {addr}"); + // These are Tower Services that will serve files from the static and pkg repos. // HandleError is needed as Axum requires services to implement Infallible Errors // because all Errors are converted into Responses let static_service = HandleError::new( ServeDir::new("./static"), handle_file_error); let pkg_service = HandleError::new( ServeDir::new("./pkg"), handle_file_error); + let cargo_leptos_service = HandleError::new( ServeDir::new(&bundle_filepath), handle_file_error); + /// Convert the Errors from ServeDir to a type that implements IntoResponse async fn handle_file_error(err: std::io::Error) -> (StatusCode, String) { @@ -40,16 +54,11 @@ if #[cfg(feature = "ssr")] { ) } - - let conf = get_configuration(Some("Cargo.toml")).await.unwrap(); - let leptos_options = conf.leptos_options; - let addr = leptos_options.site_address.clone(); - log::debug!("serving at {addr}"); - // build our application with a route let app = Router::new() .route("/api/*fn_name", post(leptos_axum::handle_server_fns)) .nest_service("/pkg", pkg_service) + .nest_service(&bundle_path, cargo_leptos_service) .nest_service("/static", static_service) .fallback(leptos_axum::render_app_to_stream(leptos_options, |cx| view! { cx, })); diff --git a/examples/todo-app-sqlite-axum/src/todo.rs b/examples/todo-app-sqlite-axum/src/todo.rs index 5b1b92b0c..0c2842fe1 100644 --- a/examples/todo-app-sqlite-axum/src/todo.rs +++ b/examples/todo-app-sqlite-axum/src/todo.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; cfg_if! { if #[cfg(feature = "ssr")] { use sqlx::{Connection, SqliteConnection}; - use http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode}; + // use http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode}; pub async fn db() -> Result { Ok(SqliteConnection::connect("sqlite:Todos.db").await.map_err(|e| ServerFnError::ServerError(e.to_string()))?) diff --git a/examples/todo-app-sqlite/Cargo.toml b/examples/todo-app-sqlite/Cargo.toml index a83e4d443..137d42e39 100644 --- a/examples/todo-app-sqlite/Cargo.toml +++ b/examples/todo-app-sqlite/Cargo.toml @@ -48,8 +48,11 @@ denylist = ["actix-files", "actix-web", "leptos_actix", "sqlx"] skip_feature_sets = [["csr", "ssr"], ["csr", "hydrate"], ["ssr", "hydrate"]] [[workspace.metadata.leptos]] +# The name of your leptos package name = "todo_app_sqlite" +# The name of the leptos server/bin package. Can be different from your lib package if they are in different crates. Otherwise, they'd be the same bin-package = "todo_app_sqlite" +# The name of the leptos frontend/lib package. Can be different from your bin package if they are in different crates. Otherwise, they'd be the same lib-package = "todo_app_sqlite" # The name used by wasm-bindgen/cargo-leptos for the JS/WASM bundle. Defaults to the crate name output-name = "todo_app_sqlite" diff --git a/examples/todo-app-sqlite/src/main.rs b/examples/todo-app-sqlite/src/main.rs index eb2673cfe..f7c0f430b 100644 --- a/examples/todo-app-sqlite/src/main.rs +++ b/examples/todo-app-sqlite/src/main.rs @@ -9,7 +9,6 @@ cfg_if! { use actix_files::{Files}; use actix_web::*; use crate::todo::*; - use std::net::SocketAddr; #[get("/style.css")] async fn css() -> impl Responder { diff --git a/examples/todo-app-sqlite/src/todo.rs b/examples/todo-app-sqlite/src/todo.rs index 1f53757f8..a546af2ec 100644 --- a/examples/todo-app-sqlite/src/todo.rs +++ b/examples/todo-app-sqlite/src/todo.rs @@ -57,8 +57,9 @@ pub async fn get_todos(cx: Scope) -> Result, ServerFnError> { Ok(todos) } - -#[server(AddTodo, "/api")] +// This is an example of leptos's server functions using an alternative CBOR encoding. Both the function arguments being sent +// to the server and the server response will be encoded with CBOR. Good for binary data that doesn't encode well via the default methods +#[server(AddTodo, "/api", "Cbor")] pub async fn add_todo(title: String) -> Result<(), ServerFnError> { let mut conn = db().await?; diff --git a/integrations/actix/src/lib.rs b/integrations/actix/src/lib.rs index 9803ae9a2..fc4e66c0d 100644 --- a/integrations/actix/src/lib.rs +++ b/integrations/actix/src/lib.rs @@ -245,10 +245,11 @@ where IV: IntoView let site_root = &options.site_root; let pkg_path = &options.site_pkg_dir; - // We need to do some logic to check if the site_root is /pkg - // if it is, then we need to not add pkg_path + // We need to do some logic to check if the site_root is pkg + // if it is, then we need to not add pkg_path. This would mean + // the site was built with cargo run and not cargo-leptos let bundle_path = match site_root.as_ref() { - "/pkg" => "/pkg".to_string(), + "pkg" => "pkg".to_string(), _ => format!("{}/{}", site_root, pkg_path), }; diff --git a/integrations/axum/src/lib.rs b/integrations/axum/src/lib.rs index 87882d440..abef36409 100644 --- a/integrations/axum/src/lib.rs +++ b/integrations/axum/src/lib.rs @@ -289,10 +289,11 @@ where let site_root = &options.site_root; let pkg_path = &options.site_pkg_dir; - // We need to do some logic to check if the site_root is /pkg - // if it is, then we need to not add pkg_path + // We need to do some logic to check if the site_root is pkg + // if it is, then we need to not add pkg_path. This would mean + // the site was built with cargo run and not cargo-leptos let bundle_path = match site_root.as_ref() { - "/pkg" => "/pkg".to_string(), + "pkg" => "pkg".to_string(), _ => format!("{}/{}", site_root, pkg_path), };