Files
dioxus/packages/liveview/src/config.rs
T
Evan AlmloffandJonathan Kelley 20d146d9bd Simplify the launch builder (#2967)
* improve documentation for the fullstack server context

* Add a section about axum integration to the crate root docs

* make serve_dioxus_application accept the cfg builder directly

* remove unused server_fn module

* improve fullstack config docs

* improve documentation for the server function macro

* fix axum router extension link

* Fix doc tests

* Fix launch builder

* Simplify the launch builder

* don't re-export launch in the prelude

* refactor fullstack launch

* Fix fullstack launch builder

* Update static generation with the new builder api

* fix some formatting/overly broad launch replacements

* fix custom menu example

* fix fullstack/static generation examples

* Fix static generation launch

* A few small formatting fixes

* Fix a few doc tests

* implement LaunchConfig for serve configs

* fix fullstack launch with separate web and server launch methods

* fix check with all features

* dont expose inner core module

* clippy and check

* fix readme

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
2024-10-10 16:00:58 -07:00

62 lines
1.8 KiB
Rust

use dioxus_core::{LaunchConfig, VirtualDom};
use crate::LiveviewRouter;
pub(crate) fn app_title() -> String {
dioxus_cli_config::app_title().unwrap_or_else(|| "Dioxus Liveview App".to_string())
}
/// A configuration for the LiveView server.
pub struct Config<R: LiveviewRouter> {
router: R,
address: std::net::SocketAddr,
route: String,
}
impl<R: LiveviewRouter + 'static> LaunchConfig for Config<R> {}
impl<R: LiveviewRouter> Default for Config<R> {
fn default() -> Self {
Self {
address: dioxus_cli_config::fullstack_address_or_localhost(),
router: R::create_default_liveview_router(),
route: "/".to_string(),
}
}
}
impl<R: LiveviewRouter> Config<R> {
/// Set the route to use for the LiveView server.
pub fn route(mut self, route: impl Into<String>) -> Self {
self.route = route.into();
self
}
/// Create a new configuration for the LiveView server.
pub fn with_app(mut self, app: fn() -> dioxus_core::prelude::Element) -> Self {
self.router = self.router.with_app(&self.route, app);
self
}
/// Create a new configuration for the LiveView server.
pub fn with_virtual_dom(
mut self,
virtual_dom: impl Fn() -> VirtualDom + Send + Sync + 'static,
) -> Self {
self.router = self.router.with_virtual_dom(&self.route, virtual_dom);
self
}
/// Set the address to listen on.
pub fn address(mut self, address: impl Into<std::net::SocketAddr>) -> Self {
self.address = address.into();
self
}
/// Launch the LiveView server.
pub async fn launch(self) {
println!("{} started on http://{}", app_title(), self.address);
self.router.start(self.address).await
}
}