More changes to the examples

This commit is contained in:
Ben Wishovich 2022-12-30 16:17:17 -08:00
parent 8eaa0b0c15
commit 442dc1e041
13 changed files with 64 additions and 25 deletions

View File

@ -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"

View File

@ -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,
<>

View File

@ -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"

View File

@ -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,
<>

View File

@ -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, <App/> }))

View File

@ -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.

View File

@ -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, <TodoApp/> }));

View File

@ -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<SqliteConnection, ServerFnError> {
Ok(SqliteConnection::connect("sqlite:Todos.db").await.map_err(|e| ServerFnError::ServerError(e.to_string()))?)

View File

@ -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"

View File

@ -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 {

View File

@ -57,8 +57,9 @@ pub async fn get_todos(cx: Scope) -> Result<Vec<Todo>, 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?;

View File

@ -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),
};

View File

@ -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),
};