Files
dioxus/packages/server/src/serve.rs
T

85 lines
2.8 KiB
Rust

use dioxus_core::Component;
#[derive(Clone)]
pub struct ServeConfigBuilder<P: Clone> {
pub(crate) app: Component<P>,
pub(crate) props: P,
pub(crate) application_name: Option<&'static str>,
pub(crate) base_path: Option<&'static str>,
pub(crate) head: Option<&'static str>,
pub(crate) assets_path: Option<&'static str>,
}
impl<P: Clone> ServeConfigBuilder<P> {
/// Create a new ServeConfig
pub fn new(app: Component<P>, props: P) -> Self {
Self {
app,
props,
application_name: None,
base_path: None,
head: None,
assets_path: None,
}
}
/// Set the application name matching the name in the Dioxus.toml file used to build the application
pub fn application_name(mut self, application_name: &'static str) -> Self {
self.application_name = Some(application_name);
self
}
/// Set the path the WASM application will be served under
pub fn base_path(mut self, base_path: &'static str) -> Self {
self.base_path = Some(base_path);
self
}
/// Set the head content to be included in the HTML document served
pub fn head(mut self, head: &'static str) -> Self {
self.head = Some(head);
self
}
/// Set the path of the assets folder generated by the Dioxus CLI. (defaults to dist/assets)
pub fn assets_path(mut self, assets_path: &'static str) -> Self {
self.assets_path = Some(assets_path);
self
}
/// Build the ServeConfig
pub fn build(self) -> ServeConfig<P> {
let base_path = self.base_path.unwrap_or(".");
let application_name = self.application_name.unwrap_or("dioxus");
ServeConfig {
app: self.app,
props: self.props,
application_name,
base_path,
head: self.head.map(String::from).unwrap_or(format!(r#"<title>Dioxus Application</title>
<link rel="preload" href="/{base_path}/assets/dioxus/{application_name}_bg.wasm" as="fetch" type="application/wasm" crossorigin="" />
<link rel="modulepreload" href="/{base_path}/assets/dioxus/{application_name}.js" />
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="UTF-8" />"#)),
assets_path: self.assets_path.unwrap_or("dist/assets"),
}
}
}
#[derive(Clone)]
pub struct ServeConfig<P: Clone> {
pub(crate) app: Component<P>,
pub(crate) props: P,
pub(crate) application_name: &'static str,
pub(crate) base_path: &'static str,
pub(crate) head: String,
pub(crate) assets_path: &'static str,
}
impl<P: Clone> From<ServeConfigBuilder<P>> for ServeConfig<P> {
fn from(builder: ServeConfigBuilder<P>) -> Self {
builder.build()
}
}