create server launch macro

This commit is contained in:
Evan Almloff committed 2023-06-23 18:23:03 -07:00
1 parent f9161f0a9b
commit 99674fcf94
20 files changed
+359 -334

No files matched your search

@@ -56,7 +56,7 @@
use axum::{
body::{self, Body, BoxBody, Full},
extract::{State, WebSocketUpgrade},
extract::State,
handler::Handler,
http::{Request, Response, StatusCode},
response::IntoResponse,
@@ -327,7 +327,7 @@ where
Router::new()
.route(
"/disconnect",
get(|ws: WebSocketUpgrade| async {
get(|ws: axum::extract::WebSocketUpgrade| async {
ws.on_upgrade(|mut ws| async move {
use axum::extract::ws::Message;
let _ = ws.send(Message::Text("connected".into())).await;
@@ -452,7 +452,7 @@ fn report_err<E: Error>(e: E) -> Response<BoxBody> {
/// A handler for Dioxus web hot reload websocket. This will send the updated static parts of the RSX to the client when they change.
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "ssr"))]
pub async fn hot_reload_handler(ws: WebSocketUpgrade) -> impl IntoResponse {
pub async fn hot_reload_handler(ws: axum::extract::WebSocketUpgrade) -> impl IntoResponse {
use axum::extract::ws::Message;
use futures_util::StreamExt;
@@ -49,7 +49,6 @@
//! }
//! ```
use dioxus_core::VirtualDom;
use hyper::{http::HeaderValue, StatusCode};
use salvo::{
async_trait, handler,
@@ -325,18 +324,22 @@ impl<P: Clone + serde::Serialize + Send + Sync + 'static> Handler for SSRHandler
let renderer_pool = if let Some(renderer) = depot.obtain::<SSRState>() {
renderer.clone()
} else {
let renderer = SSRState::default();
let renderer = SSRState::new(&self.cfg);
depot.inject(renderer.clone());
renderer
};
let parts: Arc<RequestParts> = Arc::new(extract_parts(req));
let route = parts.uri.path().to_string();
let server_context = DioxusServerContext::new(parts);
let mut vdom = VirtualDom::new_with_props(self.cfg.app, self.cfg.props.clone())
.with_root_context(server_context.clone());
let _ = vdom.rebuild();
res.write_body(renderer_pool.render_vdom(&vdom, &self.cfg))
.unwrap();
res.write_body(
renderer_pool
.render(route, &self.cfg, |vdom| {
vdom.base_scope().provide_context(server_context.clone());
})
.unwrap(),
)
.unwrap();
*res.headers_mut() = server_context.take_response_headers();
}
+17 -13
View File
@@ -50,7 +50,6 @@ use crate::{
prelude::*, render::SSRState, serve_config::ServeConfig, server_fn::DioxusServerFnRegistry,
};
use dioxus_core::VirtualDom;
use server_fn::{Encoding, Payload, ServerFunctionRegistry};
use std::error::Error;
use std::sync::Arc;
@@ -168,8 +167,13 @@ pub fn serve_dioxus_application<P: Clone + serde::Serialize + Send + Sync + 'sta
let serve_dir = warp::fs::dir(cfg.assets_path);
connect_hot_reload()
// First register the server functions
.or(register_server_fns(server_fn_route))
// Then the index route
.or(path::end().and(render_ssr(cfg.clone())))
// Then the static assets
.or(serve_dir)
// Then all other routes
.or(render_ssr(cfg))
.boxed()
}
@@ -180,17 +184,16 @@ pub fn render_ssr<P: Clone + serde::Serialize + Send + Sync + 'static>(
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
warp::get()
.and(request_parts())
.and(with_ssr_state())
.map(move |parts, renderer: SSRState| {
.and(with_ssr_state(&cfg))
.map(move |parts: RequestParts, renderer: SSRState| {
let route = parts.uri.path().to_string();
let parts = Arc::new(parts);
let server_context = DioxusServerContext::new(parts);
let mut vdom = VirtualDom::new_with_props(cfg.app, cfg.props.clone())
.with_root_context(server_context.clone());
let _ = vdom.rebuild();
let html = renderer.render_vdom(&vdom, &cfg);
let html = renderer.render(route, &cfg, |vdom| {
vdom.base_scope().provide_context(server_context.clone());
});
let mut res = Response::builder();
@@ -198,7 +201,7 @@ pub fn render_ssr<P: Clone + serde::Serialize + Send + Sync + 'static>(
server_context.take_response_headers();
res.header("Content-Type", "text/html")
.body(Bytes::from(html))
.body(Bytes::from(html.unwrap()))
.unwrap()
})
}
@@ -230,10 +233,11 @@ pub fn request_parts(
})
}
fn with_ssr_state() -> impl Filter<Extract = (SSRState,), Error = std::convert::Infallible> + Clone
{
let state = SSRState::default();
warp::any().map(move || state.clone())
fn with_ssr_state<P: Clone + serde::Serialize + Send + Sync + 'static>(
cfg: &ServeConfig<P>,
) -> impl Filter<Extract = (SSRState,), Error = std::convert::Infallible> + Clone {
let renderer = SSRState::new(cfg);
warp::any().map(move || renderer.clone())
}
#[derive(Debug)]
+158
View File
@@ -0,0 +1,158 @@
//! Launch helper macros for fullstack apps
#[macro_export]
/// Launch a server with a router
macro_rules! launch_router {
(@router_config) => {
dioxus_fullstack::router::FullstackRouterConfig::default()
};
(@router_config $router_cfg:expr) => {
$router_cfg
};
(@[$address:expr], $route:ty, $(cfg: $router_cfg:expr,)? {$($rule:ident $(: $cfg:expr)?,)*}) => {
dioxus_fullstack::launch!(
@[$address],
dioxus_fullstack::router::RouteWithCfg::<$route>,
(dioxus_fullstack::launch_router!(@router_config $($router_cfg)?)),
{
$($rule $(: $cfg)?,)*
}
)
};
}
#[macro_export]
/// Launch a server
macro_rules! launch {
(@web_cfg $server_cfg:ident $wcfg:expr) => {
#[cfg(feature = "web")]
let web_cfg = $wcfg;
};
(@web_cfg $server_cfg:ident) => {
#[cfg(feature = "web")]
let web_cfg = dioxus_web::Config::new();
};
(@server_cfg $server_cfg:ident $cfg:expr) => {
#[cfg(feature = "ssr")]
let $server_cfg = $cfg;
};
(@hot_reload $server_cfg:ident) => {
#[cfg(feature = "ssr")]
{
hot_reload_init!(dioxus_hot_reload::Config::new().with_rebuild_callback(|| {
std::process::Command::new("cargo")
.args(&["run", "--features", "ssr"])
.spawn()
.unwrap()
.wait()
.unwrap();
std::process::Command::new("cargo")
.args(&["run", "--features", "web"])
.spawn()
.unwrap()
.wait()
.unwrap();
true
}));
}
};
(@hot_reload $server_cfg:ident $hot_reload_cfg:expr) => {
#[cfg(feature = "ssr")]
{
hot_reload_init!($hot_reload_cfg);
}
};
(@incremental $server_cfg:ident) => {
#[cfg(feature = "ssr")]
let $server_cfg = $server_cfg.incremental(dioxus_fullstack::prelude::IncrementalRendererConfig::default());
};
(@incremental $server_cfg:ident $cfg:expr) => {
#[cfg(feature = "ssr")]
let $server_cfg = $server_cfg.incremental($cfg);
};
(@props_type) => {
Default::default()
};
(@props_type $props:expr) => {
$props
};
(@[$address:expr], $comp:path, $(( $props:expr ),)? {$($rule:ident $(: $cfg:expr)?,)*}) => {
#[cfg(feature = "web")]
{
#[allow(unused)]
let web_cfg = dioxus_web::Config::new();
$(
launch!(@$rule server_cfg $($cfg)?);
)*
dioxus_web::launch_with_props(
$comp,
dioxus_fullstack::prelude::get_root_props_from_document().expect("Failed to get root props from document"),
web_cfg.hydrate(true),
);
}
#[cfg(feature = "ssr")]
{
let server_cfg = ServeConfigBuilder::new($comp, launch!(@props_type $($props)?));
$(
launch!(@$rule server_cfg $($cfg)?);
)*
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
let addr = std::net::SocketAddr::from($address);
dioxus_fullstack::launch::launch_server(addr, server_cfg.build()).await;
});
}
};
}
/// Launch a server with the given configeration
/// This will use the routing intigration of the currently enabled intigration feature
#[cfg(feature = "ssr")]
pub async fn launch_server<P: Clone + serde::Serialize + Send + Sync + 'static>(
addr: std::net::SocketAddr,
cfg: crate::prelude::ServeConfig<P>,
) {
#[cfg(all(feature = "axum", not(feature = "warp"), not(feature = "salvo")))]
{
use crate::adapters::axum_adapter::DioxusRouterExt;
axum::Server::bind(&addr)
.serve(
axum::Router::new()
.serve_dioxus_application("", cfg)
.into_make_service(),
)
.await
.unwrap();
}
#[cfg(all(feature = "warp", not(feature = "axum"), not(feature = "salvo")))]
{
warp::serve(crate::prelude::serve_dioxus_application("", cfg))
.run(addr)
.await;
}
#[cfg(all(feature = "salvo", not(feature = "axum"), not(feature = "warp")))]
{
use crate::adapters::salvo_adapter::DioxusRouterExt;
let router = salvo::Router::new().serve_dioxus_application("", cfg);
salvo::Server::new(salvo::listener::TcpListener::bind(addr))
.serve(router)
.await;
}
}
+5 -2
View File
@@ -7,9 +7,13 @@ pub use adapters::*;
mod props_html;
#[cfg(feature = "router")]
pub mod router;
mod adapters;
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "ssr"))]
mod hot_reload;
pub mod launch;
#[cfg(feature = "ssr")]
mod render;
#[cfg(feature = "ssr")]
@@ -29,8 +33,6 @@ pub mod prelude {
pub use crate::props_html::deserialize_props::get_root_props_from_document;
#[cfg(feature = "ssr")]
pub use crate::render::SSRState;
#[cfg(all(feature = "router", feature = "ssr"))]
pub use crate::serve_config::FullstackRouterConfig;
#[cfg(feature = "ssr")]
pub use crate::serve_config::{ServeConfig, ServeConfigBuilder};
#[cfg(feature = "ssr")]
@@ -39,6 +41,7 @@ pub mod prelude {
pub use crate::server_fn::DioxusServerFn;
#[cfg(feature = "ssr")]
pub use crate::server_fn::{ServerFnTraitObj, ServerFunction};
pub use crate::{launch, launch_router};
pub use dioxus_server_macro::*;
#[cfg(feature = "ssr")]
pub use dioxus_ssr::incremental::IncrementalRendererConfig;
+1
View File
@@ -32,6 +32,7 @@ impl SsrRendererPool {
match self {
Self::Renderer(pool) => {
let mut vdom = VirtualDom::new_with_props(component, props);
modify_vdom(&mut vdom);
let _ = vdom.rebuild();
let mut renderer = pool.pull(pre_renderer);
+110
View File
@@ -0,0 +1,110 @@
//! Fullstack router intigration
#![allow(non_snake_case)]
use dioxus::prelude::*;
/// Used by the launch macro
#[doc(hidden)]
pub fn RouteWithCfg<R>(cx: Scope<FullstackRouterConfig<R>>) -> Element
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
use dioxus_router::prelude::RouterConfig;
#[cfg(feature = "ssr")]
let context: crate::prelude::DioxusServerContext = cx
.consume_context()
.expect("RouteWithCfg should be served by dioxus fullstack");
let cfg = *cx.props;
render! {
dioxus_router::prelude::GenericRouter::<R> {
config: move || {
RouterConfig::default()
.failure_external_navigation(cfg.failure_external_navigation)
.history({
#[cfg(feature = "ssr")]
let history = dioxus_router::prelude::MemoryHistory::with_initial_path(
context
.request_parts()
.uri
.to_string()
.parse()
.unwrap_or_else(|err| {
log::error!("Failed to parse uri: {}", err);
"/"
.parse()
.unwrap_or_else(|err| {
panic!("Failed to parse uri: {}", err);
})
}),
);
#[cfg(not(feature = "ssr"))]
let history = dioxus_router::prelude::WebHistory::new(
None,
cfg.scroll_restoration,
);
history
})
},
}
}
}
fn default_external_navigation_handler<R>() -> fn(Scope) -> Element
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
dioxus_router::prelude::FailureExternalNavigation::<R>
}
/// The configeration for the router
#[derive(Props, serde::Serialize, serde::Deserialize)]
pub struct FullstackRouterConfig<R>
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
#[serde(skip)]
#[serde(default = "default_external_navigation_handler::<R>")]
failure_external_navigation: fn(Scope) -> Element,
scroll_restoration: bool,
#[serde(skip)]
phantom: std::marker::PhantomData<R>,
}
impl<R> Clone for FullstackRouterConfig<R>
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
fn clone(&self) -> Self {
Self {
failure_external_navigation: self.failure_external_navigation,
scroll_restoration: self.scroll_restoration,
phantom: std::marker::PhantomData,
}
}
}
impl<R> Copy for FullstackRouterConfig<R>
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
}
impl<R> Default for FullstackRouterConfig<R>
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
fn default() -> Self {
Self {
failure_external_navigation: dioxus_router::prelude::FailureExternalNavigation::<R>,
scroll_restoration: true,
phantom: std::marker::PhantomData,
}
}
}
+3 -126
View File
@@ -1,6 +1,8 @@
#![allow(non_snake_case)]
//! Configeration for how to serve a Dioxus application
#[cfg(feature = "router")]
use crate::router::*;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
@@ -15,7 +17,6 @@ pub struct ServeConfigBuilder<P: Clone> {
pub(crate) root_id: Option<&'static str>,
pub(crate) index_path: Option<&'static str>,
pub(crate) assets_path: Option<&'static str>,
#[cfg(feature = "router")]
pub(crate) incremental: Option<
std::sync::Arc<
dioxus_ssr::incremental::IncrementalRendererConfig<EmptyIncrementalRenderTemplate>,
@@ -23,123 +24,10 @@ pub struct ServeConfigBuilder<P: Clone> {
>,
}
#[cfg(feature = "router")]
fn default_external_navigation_handler<R>() -> fn(Scope) -> Element
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
dioxus_router::prelude::FailureExternalNavigation::<R>
}
#[cfg(feature = "router")]
/// The configeration for the router
#[derive(Props, serde::Serialize, serde::Deserialize)]
pub struct FullstackRouterConfig<R>
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
#[serde(skip)]
#[serde(default = "default_external_navigation_handler::<R>")]
failure_external_navigation: fn(Scope) -> Element,
scroll_restoration: bool,
#[serde(skip)]
phantom: std::marker::PhantomData<R>,
}
#[cfg(feature = "router")]
impl<R> Clone for FullstackRouterConfig<R>
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
fn clone(&self) -> Self {
Self {
failure_external_navigation: self.failure_external_navigation,
scroll_restoration: self.scroll_restoration,
phantom: std::marker::PhantomData,
}
}
}
#[cfg(feature = "router")]
impl<R> Copy for FullstackRouterConfig<R>
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
}
#[cfg(feature = "router")]
impl<R> Default for FullstackRouterConfig<R>
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
fn default() -> Self {
Self {
failure_external_navigation: dioxus_router::prelude::FailureExternalNavigation::<R>,
scroll_restoration: true,
phantom: std::marker::PhantomData,
}
}
}
#[cfg(feature = "router")]
fn RouteWithCfg<R>(cx: Scope<FullstackRouterConfig<R>>) -> Element
where
R: dioxus_router::prelude::Routable,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
use dioxus_router::prelude::RouterConfig;
#[cfg(feature = "ssr")]
let context: crate::prelude::DioxusServerContext = cx
.consume_context()
.expect("RouteWithCfg should be served by dioxus fullstack");
let cfg = *cx.props;
render! {
dioxus_router::prelude::GenericRouter::<R> {
config: move || {
RouterConfig::default()
.failure_external_navigation(cfg.failure_external_navigation)
.history({
#[cfg(feature = "ssr")]
let history = dioxus_router::prelude::MemoryHistory::with_initial_path(
context
.request_parts()
.uri
.to_string()
.parse()
.unwrap_or_else(|err| {
log::error!("Failed to parse uri: {}", err);
"/"
.parse()
.unwrap_or_else(|err| {
panic!("Failed to parse uri: {}", err);
})
}),
);
#[cfg(not(feature = "ssr"))]
let history = dioxus_router::prelude::WebHistory::new(
None,
cfg.scroll_restoration,
);
history
})
},
}
}
}
#[cfg(feature = "router")]
/// A template for incremental rendering that does nothing.
#[derive(Default, Clone)]
pub struct EmptyIncrementalRenderTemplate;
#[cfg(feature = "router")]
impl dioxus_ssr::incremental::RenderHTML for EmptyIncrementalRenderTemplate {
fn render_after_body<R: std::io::Write>(
&self,
@@ -164,14 +52,7 @@ where
{
/// Create a new ServeConfigBuilder to serve a router on the server.
pub fn new_with_router(cfg: FullstackRouterConfig<R>) -> Self {
Self {
app: RouteWithCfg::<R>,
props: cfg,
root_id: None,
index_path: None,
assets_path: None,
incremental: None,
}
Self::new(RouteWithCfg::<R>, cfg)
}
}
@@ -184,12 +65,10 @@ impl<P: Clone> ServeConfigBuilder<P> {
root_id: None,
index_path: None,
assets_path: None,
#[cfg(feature = "router")]
incremental: None,
}
}
#[cfg(feature = "router")]
/// Enable incremental static generation
pub fn incremental(
mut self,
@@ -235,7 +114,6 @@ impl<P: Clone> ServeConfigBuilder<P> {
props: self.props,
index,
assets_path,
#[cfg(feature = "router")]
incremental: self.incremental,
}
}
@@ -279,7 +157,6 @@ pub struct ServeConfig<P: Clone> {
pub(crate) props: P,
pub(crate) index: IndexHtml,
pub(crate) assets_path: &'static str,
#[cfg(feature = "router")]
pub(crate) incremental: Option<
std::sync::Arc<
dioxus_ssr::incremental::IncrementalRendererConfig<EmptyIncrementalRenderTemplate>,