Make desktop fullstack work with the CLI (#2862)

* Make desktop fullstack work with the CLI
* Simplify desktop fullstack example
* move the profiles to the workspace
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-08-20 14:57:51 -07:00
1 parent 6a46a66c9a
commit 5a7a91323a
23 files changed
+239 -259

No files matched your search

+10
View File
@@ -3,3 +3,13 @@
DIOXUS_FRONT_ISSUER_URL = "" DIOXUS_FRONT_ISSUER_URL = ""
DIOXUS_FRONT_CLIENT_ID = "" DIOXUS_FRONT_CLIENT_ID = ""
DIOXUS_FRONT_URL = "" DIOXUS_FRONT_URL = ""
[profile]
[profile.dioxus-client]
inherits = "dev"
opt-level = 2
[profile.dioxus-server]
inherits = "dev"
opt-level = 2
Generated
-2
View File
@@ -3638,10 +3638,8 @@ dependencies = [
name = "fullstack-desktop-example" name = "fullstack-desktop-example"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"axum 0.7.5",
"dioxus", "dioxus",
"serde", "serde",
"tokio",
] ]
[[package]] [[package]]
+1 -1
View File
@@ -73,7 +73,7 @@ once_cell = "1.19.0"
# plugin packages # plugin packages
open = "5.0.1" open = "5.0.1"
cargo-generate = "=0.21.1" cargo-generate = "=0.21.1"
toml_edit = "0.22.15" toml_edit = "0.22.20"
# bundling # bundling
tauri-bundler = { workspace = true } tauri-bundler = { workspace = true }
+2 -1
View File
@@ -231,7 +231,7 @@ impl BuildRequest {
Ok(Some(assets)) Ok(Some(assets))
}) })
.await .await
.unwrap() .map_err(|e| anyhow::anyhow!(e))?
} }
pub fn copy_assets_dir(&self) -> anyhow::Result<()> { pub fn copy_assets_dir(&self) -> anyhow::Result<()> {
@@ -257,6 +257,7 @@ impl BuildRequest {
match self.build_arguments.platform { match self.build_arguments.platform {
Some(Platform::Fullstack | Platform::StaticGeneration) => match self.target_platform { Some(Platform::Fullstack | Platform::StaticGeneration) => match self.target_platform {
TargetPlatform::Web => out_dir.join("public"), TargetPlatform::Web => out_dir.join("public"),
TargetPlatform::Desktop => out_dir.join("desktop"),
_ => out_dir, _ => out_dir,
}, },
_ => out_dir, _ => out_dir,
+66 -43
View File
@@ -1,41 +1,60 @@
use toml_edit::Item;
use crate::builder::Build; use crate::builder::Build;
use crate::dioxus_crate::DioxusCrate; use crate::dioxus_crate::DioxusCrate;
use crate::builder::BuildRequest; use crate::builder::BuildRequest;
use std::path::PathBuf; use std::io::Write;
use super::TargetPlatform; use super::TargetPlatform;
static CLIENT_RUST_FLAGS: &[&str] = &["-Cdebuginfo=none", "-Cstrip=debuginfo"]; static CLIENT_PROFILE: &str = "dioxus-client";
static SERVER_PROFILE: &str = "dioxus-server";
// The `opt-level=2` increases build times, but can noticeably decrease time // The `opt-level=2` increases build times, but can noticeably decrease time
// between saving changes and being able to interact with an app. The "overall" // between saving changes and being able to interact with an app. The "overall"
// time difference (between having and not having the optimization) can be // time difference (between having and not having the optimization) can be
// almost imperceptible (~1 s) but also can be very noticeable (~6 s) — depends // almost imperceptible (~1 s) but also can be very noticeable (~6 s) — depends
// on setup (hardware, OS, browser, idle load). // on setup (hardware, OS, browser, idle load).
static SERVER_RUST_FLAGS: &[&str] = &["-O"]; // Find or create the client and server profiles in the .cargo/config.toml file
static DEBUG_RUST_FLAG: &str = "-Cdebug-assertions"; fn initialize_profiles(config: &DioxusCrate) -> crate::Result<()> {
let config_path = config.workspace_dir().join(".cargo/config.toml");
let mut config = match std::fs::read_to_string(&config_path) {
Ok(config) => config.parse::<toml_edit::DocumentMut>().map_err(|e| {
crate::Error::Other(anyhow::anyhow!("Failed to parse .cargo/config.toml: {}", e))
})?,
Err(_) => Default::default(),
};
fn add_debug_rust_flags(build: &Build, flags: &mut Vec<String>) { if let Item::Table(table) = config
if !build.release { .as_table_mut()
flags.push(DEBUG_RUST_FLAG.to_string()); .entry("profile")
} .or_insert(Item::Table(Default::default()))
} {
if let toml_edit::Entry::Vacant(entry) = table.entry(CLIENT_PROFILE) {
let mut client = toml_edit::Table::new();
client.insert("inherits", Item::Value("dev".into()));
client.insert("opt-level", Item::Value(2.into()));
entry.insert(Item::Table(client));
}
fn fullstack_rust_flags(build: &Build, base_flags: &[&str]) -> Vec<String> { if let toml_edit::Entry::Vacant(entry) = table.entry(SERVER_PROFILE) {
// If we are forcing debug mode, don't add any debug flags let mut server = toml_edit::Table::new();
if build.force_debug { server.insert("inherits", Item::Value("dev".into()));
return Default::default(); server.insert("opt-level", Item::Value(2.into()));
entry.insert(Item::Table(server));
}
} }
let mut rust_flags = base_flags.iter().map(ToString::to_string).collect(); // Write the config back to the file
add_debug_rust_flags(build, &mut rust_flags); if let Some(parent) = config_path.parent() {
rust_flags std::fs::create_dir_all(parent)?;
} }
let file = std::fs::File::create(config_path)?;
let mut buf_writer = std::io::BufWriter::new(file);
write!(buf_writer, "{}", config)?;
// Fullstack builds run the server and client builds parallel by default Ok(())
// To make them run in parallel, we need to set up different target directories for the server and client within /.dioxus
fn get_target_directory(build: &Build, target: PathBuf) -> Option<PathBuf> {
(!build.force_sequential).then_some(target)
} }
impl BuildRequest { impl BuildRequest {
@@ -43,63 +62,67 @@ impl BuildRequest {
config: DioxusCrate, config: DioxusCrate,
build_arguments: Build, build_arguments: Build,
serve: bool, serve: bool,
) -> Vec<Self> { ) -> Result<Vec<Self>, crate::Error> {
vec![ initialize_profiles(&config)?;
Ok(vec![
Self::new_client(serve, &config, &build_arguments), Self::new_client(serve, &config, &build_arguments),
Self::new_server(serve, &config, &build_arguments), Self::new_server(serve, &config, &build_arguments),
] ])
} }
fn new_with_target_directory_rust_flags_and_features( fn new_with_target_directory_rust_flags_and_features(
serve: bool, serve: bool,
config: &DioxusCrate, config: &DioxusCrate,
build: &Build, build: &Build,
target_directory: PathBuf, feature: Option<String>,
rust_flags: &[&str],
feature: String,
target_platform: TargetPlatform, target_platform: TargetPlatform,
) -> Self { ) -> Self {
let config = config.clone(); let config = config.clone();
let mut build = build.clone(); let mut build = build.clone();
// Set the target directory we are building the server in
let target_dir = get_target_directory(&build, target_directory);
// Add the server feature to the features we pass to the build // Add the server feature to the features we pass to the build
build.target_args.features.push(feature); if let Some(feature) = feature {
build.target_args.features.push(feature);
}
// Add the server flags to the build arguments // Add the server flags to the build arguments
let rust_flags = fullstack_rust_flags(&build, rust_flags);
Self { Self {
serve, serve,
build_arguments: build.clone(), build_arguments: build.clone(),
dioxus_crate: config, dioxus_crate: config,
rust_flags, rust_flags: Default::default(),
target_dir, target_dir: None,
target_platform, target_platform,
} }
} }
fn new_server(serve: bool, config: &DioxusCrate, build: &Build) -> Self { fn new_server(serve: bool, config: &DioxusCrate, build: &Build) -> Self {
let mut build = build.clone();
if build.profile.is_none() {
build.profile = Some(CLIENT_PROFILE.to_string());
}
let client_feature = build.auto_detect_server_feature(config);
Self::new_with_target_directory_rust_flags_and_features( Self::new_with_target_directory_rust_flags_and_features(
serve, serve,
config, config,
build, &build,
config.server_target_dir(), build.target_args.server_feature.clone().or(client_feature),
SERVER_RUST_FLAGS,
build.target_args.server_feature.clone(),
TargetPlatform::Server, TargetPlatform::Server,
) )
} }
fn new_client(serve: bool, config: &DioxusCrate, build: &Build) -> Self { fn new_client(serve: bool, config: &DioxusCrate, build: &Build) -> Self {
let mut build = build.clone();
if build.profile.is_none() {
build.profile = Some(SERVER_PROFILE.to_string());
}
let (client_feature, client_platform) = build.auto_detect_client_platform(config);
Self::new_with_target_directory_rust_flags_and_features( Self::new_with_target_directory_rust_flags_and_features(
serve, serve,
config, config,
build, &build,
config.client_target_dir(), build.target_args.client_feature.clone().or(client_feature),
CLIENT_RUST_FLAGS, client_platform,
build.target_args.client_feature.clone(),
TargetPlatform::Web,
) )
} }
} }
+20 -5
View File
@@ -6,6 +6,7 @@ use dioxus_cli_config::{Platform, RuntimeCLIArguments};
use futures_util::stream::select_all; use futures_util::stream::select_all;
use futures_util::StreamExt; use futures_util::StreamExt;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::str::FromStr;
use std::{path::PathBuf, process::Stdio}; use std::{path::PathBuf, process::Stdio};
use tokio::process::{Child, Command}; use tokio::process::{Child, Command};
@@ -29,6 +30,20 @@ pub enum TargetPlatform {
Liveview, Liveview,
} }
impl FromStr for TargetPlatform {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"web" => Ok(Self::Web),
"desktop" => Ok(Self::Desktop),
"axum" | "server" => Ok(Self::Server),
"liveview" => Ok(Self::Liveview),
_ => Err(()),
}
}
}
impl std::fmt::Display for TargetPlatform { impl std::fmt::Display for TargetPlatform {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
@@ -62,7 +77,7 @@ impl BuildRequest {
serve: bool, serve: bool,
dioxus_crate: &DioxusCrate, dioxus_crate: &DioxusCrate,
build_arguments: impl Into<Build>, build_arguments: impl Into<Build>,
) -> Vec<Self> { ) -> crate::Result<Vec<Self>> {
let build_arguments = build_arguments.into(); let build_arguments = build_arguments.into();
let platform = build_arguments.platform(); let platform = build_arguments.platform();
let single_platform = |platform| { let single_platform = |platform| {
@@ -76,15 +91,15 @@ impl BuildRequest {
target_dir: Default::default(), target_dir: Default::default(),
}] }]
}; };
match platform { Ok(match platform {
Platform::Web => single_platform(TargetPlatform::Web),
Platform::Liveview => single_platform(TargetPlatform::Liveview), Platform::Liveview => single_platform(TargetPlatform::Liveview),
Platform::Web => single_platform(TargetPlatform::Web),
Platform::Desktop => single_platform(TargetPlatform::Desktop), Platform::Desktop => single_platform(TargetPlatform::Desktop),
Platform::StaticGeneration | Platform::Fullstack => { Platform::StaticGeneration | Platform::Fullstack => {
Self::new_fullstack(dioxus_crate.clone(), build_arguments, serve) Self::new_fullstack(dioxus_crate.clone(), build_arguments, serve)?
} }
_ => unimplemented!("Unknown platform: {platform:?}"), _ => unimplemented!("Unknown platform: {platform:?}"),
} })
} }
pub(crate) async fn build_all_parallel( pub(crate) async fn build_all_parallel(
+75 -9
View File
@@ -1,7 +1,12 @@
use std::str::FromStr;
use anyhow::Context; use anyhow::Context;
use dioxus_cli_config::Platform; use dioxus_cli_config::Platform;
use crate::{builder::BuildRequest, dioxus_crate::DioxusCrate}; use crate::{
builder::{BuildRequest, TargetPlatform},
dioxus_crate::DioxusCrate,
};
use super::*; use super::*;
@@ -29,12 +34,12 @@ pub struct TargetArgs {
pub features: Vec<String>, pub features: Vec<String>,
/// The feature to use for the client in a fullstack app [default: "web"] /// The feature to use for the client in a fullstack app [default: "web"]
#[clap(long, default_value_t = { "web".to_string() })] #[clap(long)]
pub client_feature: String, pub client_feature: Option<String>,
/// The feature to use for the server in a fullstack app [default: "server"] /// The feature to use for the server in a fullstack app [default: "server"]
#[clap(long, default_value_t = { "server".to_string() })] #[clap(long)]
pub server_feature: String, pub server_feature: Option<String>,
/// Rustc platform triple /// Rustc platform triple
#[clap(long)] #[clap(long)]
@@ -109,7 +114,7 @@ impl Build {
pub async fn build(&mut self, dioxus_crate: &mut DioxusCrate) -> Result<()> { pub async fn build(&mut self, dioxus_crate: &mut DioxusCrate) -> Result<()> {
self.resolve(dioxus_crate)?; self.resolve(dioxus_crate)?;
let build_requests = BuildRequest::create(false, dioxus_crate, self.clone()); let build_requests = BuildRequest::create(false, dioxus_crate, self.clone())?;
BuildRequest::build_all_parallel(build_requests).await?; BuildRequest::build_all_parallel(build_requests).await?;
Ok(()) Ok(())
} }
@@ -121,7 +126,47 @@ impl Build {
Ok(()) Ok(())
} }
pub(crate) fn auto_detect_client_platform(
&self,
resolved: &DioxusCrate,
) -> (Option<String>, TargetPlatform) {
self.find_dioxus_feature(resolved, |platform| {
matches!(platform, TargetPlatform::Web | TargetPlatform::Desktop)
})
.unwrap_or_else(|| (Some("web".to_string()), TargetPlatform::Web))
}
pub(crate) fn auto_detect_server_feature(&self, resolved: &DioxusCrate) -> Option<String> {
self.find_dioxus_feature(resolved, |platform| {
matches!(platform, TargetPlatform::Server)
})
.map(|(feature, _)| feature)
.unwrap_or_else(|| Some("server".to_string()))
}
fn auto_detect_platform(&self, resolved: &DioxusCrate) -> Platform { fn auto_detect_platform(&self, resolved: &DioxusCrate) -> Platform {
self.auto_detect_platform_with_filter(resolved, |_| true).1
}
fn auto_detect_platform_with_filter(
&self,
resolved: &DioxusCrate,
filter_platform: fn(&Platform) -> bool,
) -> (Option<String>, Platform) {
self.find_dioxus_feature(resolved, filter_platform)
.unwrap_or_else(|| {
let default_platform = resolved.dioxus_config.application.default_platform;
(Some(default_platform.to_string()), default_platform)
})
}
fn find_dioxus_feature<P: FromStr>(
&self,
resolved: &DioxusCrate,
filter_platform: fn(&P) -> bool,
) -> Option<(Option<String>, P)> {
// First check the enabled features for any renderer enabled
for dioxus in resolved.krates.krates_by_name("dioxus") { for dioxus in resolved.krates.krates_by_name("dioxus") {
let Some(features) = resolved.krates.get_enabled_features(dioxus.kid) else { let Some(features) = resolved.krates.get_enabled_features(dioxus.kid) else {
continue; continue;
@@ -129,13 +174,34 @@ impl Build {
if let Some(platform) = features if let Some(platform) = features
.iter() .iter()
.find_map(|platform| platform.parse::<Platform>().ok()) .find_map(|platform| platform.parse::<P>().ok())
.filter(filter_platform)
{ {
return platform; return Some((None, platform));
} }
} }
resolved.dioxus_config.application.default_platform // Then check the features that might get enabled
if let Some(platform) = resolved
.package()
.features
.iter()
.find_map(|(feature, enables)| {
enables
.iter()
.find_map(|f| {
f.strip_prefix("dioxus/")
.or_else(|| feature.strip_prefix("dep:dioxus/"))
.and_then(|f| f.parse::<P>().ok())
.filter(filter_platform)
})
.map(|platform| (Some(feature.clone()), platform))
})
{
return Some(platform);
}
None
} }
/// Get the platform from the build arguments /// Get the platform from the build arguments
-6
View File
@@ -29,12 +29,6 @@ impl Clean {
remove_dir_all(out_dir)?; remove_dir_all(out_dir)?;
} }
let fullstack_out_dir = dioxus_crate.fullstack_out_dir();
if fullstack_out_dir.is_dir() {
remove_dir_all(fullstack_out_dir)?;
}
Ok(()) Ok(())
} }
} }
-16
View File
@@ -192,22 +192,6 @@ impl DioxusCrate {
.join(&self.dioxus_config.application.out_dir) .join(&self.dioxus_config.application.out_dir)
} }
/// Compose an out directory for the fullstack platform. See `out_dir()`
/// method.
pub fn fullstack_out_dir(&self) -> PathBuf {
self.workspace_dir().join(".dioxus")
}
/// Compose a target directory for the server (fullstack-only?).
pub fn server_target_dir(&self) -> PathBuf {
self.fullstack_out_dir().join("ssr")
}
/// Compose a target directory for the client (fullstack-only?).
pub fn client_target_dir(&self) -> PathBuf {
self.fullstack_out_dir().join("web")
}
/// Get the workspace directory for the crate /// Get the workspace directory for the crate
pub fn workspace_dir(&self) -> PathBuf { pub fn workspace_dir(&self) -> PathBuf {
self.krates.workspace_root().as_std_path().to_path_buf() self.krates.workspace_root().as_std_path().to_path_buf()
+4 -2
View File
@@ -49,10 +49,10 @@ impl Builder {
} }
/// Start a new build - killing the current one if it exists /// Start a new build - killing the current one if it exists
pub fn build(&mut self) { pub fn build(&mut self) -> Result<()> {
self.shutdown(); self.shutdown();
let build_requests = let build_requests =
BuildRequest::create(true, &self.config, self.serve.build_arguments.clone()); BuildRequest::create(true, &self.config, self.serve.build_arguments.clone())?;
let mut set = tokio::task::JoinSet::new(); let mut set = tokio::task::JoinSet::new();
@@ -85,6 +85,8 @@ impl Builder {
} }
Ok(all_results) Ok(all_results)
})); }));
Ok(())
} }
/// Wait for any new updates to the builder - either it completed or gave us a message etc /// Wait for any new updates to the builder - either it completed or gave us a message etc
+3 -3
View File
@@ -57,7 +57,7 @@ pub async fn serve_all(
let mut builder = Builder::new(&dioxus_crate, &serve); let mut builder = Builder::new(&dioxus_crate, &serve);
// Start the first build // Start the first build
builder.build(); builder.build()?;
let mut server = Server::start(&serve, &dioxus_crate); let mut server = Server::start(&serve, &dioxus_crate);
let mut watcher = Watcher::start(&serve, &dioxus_crate); let mut watcher = Watcher::start(&serve, &dioxus_crate);
@@ -94,7 +94,7 @@ pub async fn serve_all(
} else { } else {
// If the change is not binary patchable, rebuild the project // If the change is not binary patchable, rebuild the project
// We're going to kick off a new build, interrupting the current build if it's ongoing // We're going to kick off a new build, interrupting the current build if it's ongoing
builder.build(); builder.build()?;
// Clear the hot reload changes // Clear the hot reload changes
watcher.clear_hot_reload_changes(); watcher.clear_hot_reload_changes();
@@ -196,7 +196,7 @@ pub async fn serve_all(
Ok(false) => {} Ok(false) => {}
// Request a rebuild. // Request a rebuild.
Ok(true) => { Ok(true) => {
builder.build(); builder.build()?;
server.start_build().await server.start_build().await
}, },
// Shutdown the server. // Shutdown the server.
-1
View File
@@ -57,7 +57,6 @@ impl Watcher {
"target", "target",
"node_modules", "node_modules",
"dist", "dist",
".dioxus",
&out_dir_str, &out_dir_str,
]; ];
for path in excluded_paths { for path in excluded_paths {
+1 -1
View File
@@ -49,7 +49,7 @@ fn main() {
// build our application with some routes // build our application with some routes
let app = Router::new() let app = Router::new()
// Server side render the application, serve static assets, and register server functions // Server side render the application, serve static assets, and register server functions
.serve_dioxus_application(ServeConfig::default(), app) .serve_dioxus_application(ServeConfig::new().unwrap(), app)
.layer( .layer(
axum_session_auth::AuthSessionLayer::< axum_session_auth::AuthSessionLayer::<
crate::auth::User, crate::auth::User,
+1 -15
View File
@@ -4,25 +4,11 @@ version = "0.1.0"
edition = "2021" edition = "2021"
publish = false publish = false
[lib]
[dependencies] [dependencies]
dioxus = { workspace = true, features = ["launch", "fullstack"] } dioxus = { workspace = true, features = ["launch", "fullstack"] }
axum = { workspace = true, optional = true }
tokio = { workspace = true, features = ["full"], optional = true }
serde = "1.0.159" serde = "1.0.159"
[features] [features]
default = [] default = []
server = ["axum", "tokio", "dioxus/axum"] server = ["dioxus/server"]
desktop = ["dioxus/desktop"] desktop = ["dioxus/desktop"]
[[bin]]
name = "client"
path = "src/client.rs"
required-features = ["desktop"]
[[bin]]
name = "server"
path = "src/server.rs"
required-features = ["server"]
@@ -1,15 +0,0 @@
// Run with:
// ```bash
// cargo run --bin client --features desktop
// ```
use fullstack_desktop_example::*;
fn main() {
// Set the url of the server where server functions are hosted.
#[cfg(not(feature = "server"))]
dioxus::fullstack::prelude::server_fn::client::set_server_url("http://127.0.0.1:8080");
#[cfg(feature = "desktop")]
dioxus::prelude::launch_desktop(app)
}
@@ -1,36 +0,0 @@
#![allow(non_snake_case)]
use dioxus::prelude::*;
pub fn app() -> Element {
let mut count = use_signal(|| 0);
let mut text = use_signal(|| "...".to_string());
rsx! {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
button {
onclick: move |_| async move {
if let Ok(data) = get_server_data().await {
println!("Client received: {}", data);
text.set(data.clone());
post_server_data(data).await.unwrap();
}
},
"Run a server function"
}
"Server said: {text}"
}
}
#[server(PostServerData)]
async fn post_server_data(data: String) -> Result<(), ServerFnError> {
println!("Server received: {}", data);
Ok(())
}
#[server(GetServerData)]
async fn get_server_data() -> Result<String, ServerFnError> {
Ok("Hello from the server!".to_string())
}
@@ -1,42 +1,13 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use dioxus::prelude::*; use dioxus::prelude::*;
/// Run with `cargo run --features desktop`
#[cfg(feature = "desktop")]
fn main() { fn main() {
// Set the url of the server where server functions are hosted. // Set the url of the server where server functions are hosted.
#[cfg(not(feature = "server"))]
dioxus::fullstack::prelude::server_fn::client::set_server_url("http://127.0.0.1:8080"); dioxus::fullstack::prelude::server_fn::client::set_server_url("http://127.0.0.1:8080");
launch(app);
// And then launch the app
dioxus::prelude::launch_desktop(app);
} }
/// Run with `cargo run --features server`
#[cfg(all(feature = "server", not(feature = "desktop")))]
#[tokio::main]
async fn main() {
use server_fn::axum::register_explicit;
let listener = tokio::net::TcpListener::bind("127.0.0.01:8080")
.await
.unwrap();
register_explicit::<PostServerData>();
register_explicit::<GetServerData>();
axum::serve(
listener,
axum::Router::new()
.register_server_functions()
.into_make_service(),
)
.await
.unwrap();
}
#[cfg(all(not(feature = "desktop"), not(feature = "server")))]
fn main() {}
pub fn app() -> Element { pub fn app() -> Element {
let mut count = use_signal(|| 0); let mut count = use_signal(|| 0);
let mut text = use_signal(|| "...".to_string()); let mut text = use_signal(|| "...".to_string());
@@ -1,27 +0,0 @@
// Run with:
// ```bash
// cargo run --bin server --features server
// ```
use dioxus::prelude::*;
use fullstack_desktop_example::*;
use server_fn::axum::register_explicit;
#[tokio::main]
async fn main() {
let listener = tokio::net::TcpListener::bind("127.0.0.01:8080")
.await
.unwrap();
register_explicit::<PostServerData>();
register_explicit::<GetServerData>();
axum::serve(
listener,
axum::Router::new()
.register_server_functions()
.into_make_service(),
)
.await
.unwrap();
}
+8 -10
View File
@@ -21,7 +21,7 @@
//! listener, //! listener,
//! axum::Router::new() //! axum::Router::new()
//! // Server side render the application, serve static assets, and register server functions //! // Server side render the application, serve static assets, and register server functions
//! .serve_dioxus_application(ServeConfig::default(), app) //! .serve_dioxus_application(ServeConfig::new().unwrap(), app)
//! .into_make_service(), //! .into_make_service(),
//! ) //! )
//! .await //! .await
@@ -150,7 +150,7 @@ pub trait DioxusRouterExt<S> {
/// let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080)); /// let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
/// let router = axum::Router::new() /// let router = axum::Router::new()
/// // Server side render the application, serve static assets, and register server functions /// // Server side render the application, serve static assets, and register server functions
/// .serve_dioxus_application(ServeConfig::default(), app) /// .serve_dioxus_application(ServeConfig::new().unwrap(), app)
/// .into_make_service(); /// .into_make_service();
/// let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); /// let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
/// axum::serve(listener, router).await.unwrap(); /// axum::serve(listener, router).await.unwrap();
@@ -252,11 +252,8 @@ where
let server = self.serve_static_assets().register_server_functions(); let server = self.serve_static_assets().register_server_functions();
server.fallback( server.fallback(
get(render_handler).with_state( get(render_handler)
RenderHandleState::new(app) .with_state(RenderHandleState::new(cfg, app).with_ssr_state(ssr_state)),
.with_config(cfg)
.with_ssr_state(ssr_state),
),
) )
} }
} }
@@ -281,9 +278,9 @@ pub struct RenderHandleState {
impl RenderHandleState { impl RenderHandleState {
/// Create a new [`RenderHandleState`] /// Create a new [`RenderHandleState`]
pub fn new(root: fn() -> Element) -> Self { pub fn new(config: ServeConfig, root: fn() -> Element) -> Self {
Self { Self {
config: ServeConfig::default(), config,
build_virtual_dom: Arc::new(move || VirtualDom::new(root)), build_virtual_dom: Arc::new(move || VirtualDom::new(root)),
ssr_state: Default::default(), ssr_state: Default::default(),
} }
@@ -291,10 +288,11 @@ impl RenderHandleState {
/// Create a new [`RenderHandleState`] with a custom [`VirtualDom`] factory. This method can be used to pass context into the root component of your application. /// Create a new [`RenderHandleState`] with a custom [`VirtualDom`] factory. This method can be used to pass context into the root component of your application.
pub fn new_with_virtual_dom_factory( pub fn new_with_virtual_dom_factory(
config: ServeConfig,
build_virtual_dom: impl Fn() -> VirtualDom + Send + Sync + 'static, build_virtual_dom: impl Fn() -> VirtualDom + Send + Sync + 'static,
) -> Self { ) -> Self {
Self { Self {
config: ServeConfig::default(), config,
build_virtual_dom: Arc::new(build_virtual_dom), build_virtual_dom: Arc::new(build_virtual_dom),
ssr_state: Default::default(), ssr_state: Default::default(),
} }
+18 -12
View File
@@ -146,24 +146,30 @@ async fn launch_server(
{ {
use crate::axum_adapter::DioxusRouterExt; use crate::axum_adapter::DioxusRouterExt;
let router = axum::Router::new().register_server_functions_with_context(context_providers); #[allow(unused_mut)]
let mut router =
axum::Router::new().register_server_functions_with_context(context_providers);
#[cfg(not(any(feature = "desktop", feature = "mobile")))] #[cfg(not(any(feature = "desktop", feature = "mobile")))]
let router = { {
use crate::prelude::RenderHandleState; use crate::prelude::RenderHandleState;
use crate::prelude::SSRState; use crate::prelude::SSRState;
let cfg = platform_config.server_cfg.build(); match platform_config.server_cfg.build() {
Ok(cfg) => {
router = router.serve_static_assets();
let mut router = router.serve_static_assets(); router = router.fallback(
axum::routing::get(crate::axum_adapter::render_handler).with_state(
router.fallback( RenderHandleState::new_with_virtual_dom_factory(cfg, build_virtual_dom),
axum::routing::get(crate::axum_adapter::render_handler).with_state( ),
RenderHandleState::new_with_virtual_dom_factory(build_virtual_dom) );
.with_config(cfg), }
), Err(err) => {
) tracing::trace!("Failed to create render handler. This is expected if you are only using fullstack for desktop/mobile server functions: {}", err);
}; }
}
}
let router = router.into_make_service(); let router = router.into_make_service();
let listener = tokio::net::TcpListener::bind(address).await.unwrap(); let listener = tokio::net::TcpListener::bind(address).await.unwrap();
-1
View File
@@ -418,7 +418,6 @@ impl SSRState {
} }
/// The template that wraps the body of the HTML for a fullstack page. This template contains the data needed to hydrate server functions that were run on the server. /// The template that wraps the body of the HTML for a fullstack page. This template contains the data needed to hydrate server functions that were run on the server.
#[derive(Default)]
pub struct FullstackHTMLTemplate { pub struct FullstackHTMLTemplate {
cfg: ServeConfig, cfg: ServeConfig,
} }
+27 -21
View File
@@ -50,7 +50,7 @@ impl ServeConfigBuilder {
} }
/// Build the ServeConfig /// Build the ServeConfig
pub fn build(self) -> ServeConfig { pub fn build(self) -> Result<ServeConfig, UnableToLoadIndex> {
// The CLI always bundles static assets into the exe/public directory // The CLI always bundles static assets into the exe/public directory
let public_path = public_path(); let public_path = public_path();
@@ -61,16 +61,17 @@ impl ServeConfigBuilder {
let root_id = self.root_id.unwrap_or("main"); let root_id = self.root_id.unwrap_or("main");
let index_html = self let index_html = match self.index_html {
.index_html Some(index) => index,
.unwrap_or_else(|| load_index_path(index_path)); None => load_index_path(index_path)?,
};
let index = load_index_html(index_html, root_id); let index = load_index_html(index_html, root_id);
ServeConfig { Ok(ServeConfig {
index, index,
incremental: self.incremental, incremental: self.incremental,
} })
} }
} }
@@ -84,13 +85,25 @@ pub(crate) fn public_path() -> PathBuf {
.join("public") .join("public")
} }
fn load_index_path(path: PathBuf) -> String { /// An error that can occur when loading the index.html file
let mut file = File::open(&path).unwrap_or_else(|_| panic!("Failed to find index.html. Make sure the index_path is set correctly and the WASM application has been built. Tried to open file at path: {path:?}")); #[derive(Debug)]
pub struct UnableToLoadIndex(PathBuf);
impl std::fmt::Display for UnableToLoadIndex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Failed to find index.html. Make sure the index_path is set correctly and the WASM application has been built. Tried to open file at path: {:?}", self.0)
}
}
impl std::error::Error for UnableToLoadIndex {}
fn load_index_path(path: PathBuf) -> Result<String, UnableToLoadIndex> {
let mut file = File::open(&path).map_err(|_| UnableToLoadIndex(path))?;
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents) file.read_to_string(&mut contents)
.expect("Failed to read index.html"); .expect("Failed to read index.html");
contents Ok(contents)
} }
fn load_index_html(contents: String, root_id: &'static str) -> IndexHtml { fn load_index_html(contents: String, root_id: &'static str) -> IndexHtml {
@@ -156,21 +169,14 @@ pub struct ServeConfig {
pub(crate) incremental: Option<dioxus_ssr::incremental::IncrementalRendererConfig>, pub(crate) incremental: Option<dioxus_ssr::incremental::IncrementalRendererConfig>,
} }
impl Default for ServeConfig {
fn default() -> Self {
Self::builder().build()
}
}
impl ServeConfig { impl ServeConfig {
/// Create a new ServeConfig
pub fn new() -> Result<Self, UnableToLoadIndex> {
ServeConfigBuilder::new().build()
}
/// Create a new builder for a ServeConfig /// Create a new builder for a ServeConfig
pub fn builder() -> ServeConfigBuilder { pub fn builder() -> ServeConfigBuilder {
ServeConfigBuilder::new() ServeConfigBuilder::new()
} }
} }
impl From<ServeConfigBuilder> for ServeConfig {
fn from(builder: ServeConfigBuilder) -> Self {
builder.build()
}
}
+1 -1
View File
@@ -172,7 +172,7 @@ impl Config {
} }
let cfg = cfg_builder.build(); let cfg = cfg_builder.build();
FullstackHTMLTemplate::new(&cfg) FullstackHTMLTemplate::new(&cfg.unwrap())
} }
pub(crate) fn create_cache(&mut self) -> dioxus_ssr::incremental::IncrementalRenderer { pub(crate) fn create_cache(&mut self) -> dioxus_ssr::incremental::IncrementalRenderer {