diff --git a/packages/fullstack/src/adapters/axum_adapter.rs b/packages/fullstack/src/axum_adapter.rs similarity index 99% rename from packages/fullstack/src/adapters/axum_adapter.rs rename to packages/fullstack/src/axum_adapter.rs index e12590bed..c56864728 100644 --- a/packages/fullstack/src/adapters/axum_adapter.rs +++ b/packages/fullstack/src/axum_adapter.rs @@ -70,7 +70,7 @@ use std::sync::RwLock; use crate::{ prelude::*, render::SSRState, serve_config::ServeConfig, server_context::DioxusServerContext, - server_fn::DioxusServerFnRegistry, + server_fn::collection::DioxusServerFnRegistry, }; /// A extension trait with utilities for integrating Dioxus with your Axum router. diff --git a/packages/fullstack/src/config.rs b/packages/fullstack/src/config.rs index 176e7381e..d03c90752 100644 --- a/packages/fullstack/src/config.rs +++ b/packages/fullstack/src/config.rs @@ -124,7 +124,7 @@ impl Config { #[cfg(feature = "axum")] { - use crate::adapters::axum_adapter::{render_handler, DioxusRouterExt}; + use crate::axum_adapter::{render_handler, DioxusRouterExt}; use axum::routing::get; use tower::ServiceBuilder; diff --git a/packages/fullstack/src/launch.rs b/packages/fullstack/src/launch.rs index 70deaa981..f32418f67 100644 --- a/packages/fullstack/src/launch.rs +++ b/packages/fullstack/src/launch.rs @@ -7,6 +7,7 @@ use dioxus_lib::prelude::{Element, VirtualDom}; pub use crate::Config; /// Launch a fullstack app with the given root component, contexts, and config. +#[allow(unused)] pub fn launch( root: fn() -> Element, contexts: Vec Box + Send + Sync>>, diff --git a/packages/fullstack/src/lib.rs b/packages/fullstack/src/lib.rs index 40e821dc0..8ba30a5be 100644 --- a/packages/fullstack/src/lib.rs +++ b/packages/fullstack/src/lib.rs @@ -8,17 +8,13 @@ pub use once_cell; mod html_storage; -#[cfg(feature = "server")] -mod adapters; - -// Splitting up the glob export lets us document features required for each adapter #[cfg_attr(docsrs, doc(cfg(feature = "axum")))] #[cfg(feature = "axum")] -pub use adapters::axum_adapter; +mod axum_adapter; #[cfg_attr(docsrs, doc(cfg(feature = "server")))] #[cfg(feature = "server")] -pub use adapters::{server_fn_service, ServerFnHandler}; +pub use server_fn::service::{server_fn_service, ServerFnHandler}; mod config; mod hooks; @@ -48,7 +44,7 @@ pub mod prelude { #[cfg(feature = "axum")] #[cfg_attr(docsrs, doc(cfg(feature = "axum")))] - pub use crate::adapters::axum_adapter::*; + pub use crate::axum_adapter::*; #[cfg(not(feature = "server"))] #[cfg_attr(docsrs, doc(cfg(not(feature = "server"))))] @@ -87,7 +83,7 @@ pub mod prelude { #[cfg(feature = "server")] #[cfg_attr(docsrs, doc(cfg(feature = "server")))] - pub use crate::server_fn::{ServerFnMiddleware, ServerFnTraitObj, ServerFunction}; + pub use crate::server_fn::collection::{ServerFnMiddleware, ServerFnTraitObj, ServerFunction}; pub use dioxus_server_macro::*; #[cfg(feature = "server")] diff --git a/packages/fullstack/src/server_fn.rs b/packages/fullstack/src/server_fn/collection.rs similarity index 86% rename from packages/fullstack/src/server_fn.rs rename to packages/fullstack/src/server_fn/collection.rs index cdfff266e..6ecc1b78f 100644 --- a/packages/fullstack/src/server_fn.rs +++ b/packages/fullstack/src/server_fn/collection.rs @@ -171,23 +171,3 @@ pub enum ServerRegistrationFnError { #[error("The server function registry is poisoned: {0}")] Poisoned(String), } - -/// Defines a "server function." A server function can be called from the server or the client, -/// but the body of its code will only be run on the server, i.e., if a crate feature `ssr` is enabled. -/// -/// Server functions are created using the `server` macro. -/// -/// The set of server functions -/// can be queried on the server for routing purposes by calling [server_fn::ServerFunctionRegistry::get]. -/// -/// Technically, the trait is implemented on a type that describes the server function's arguments, not the function itself. -pub trait DioxusServerFn: server_fn::ServerFn<()> { - /// Registers the server function, allowing the client to query it by URL. - #[cfg(feature = "server")] - #[cfg_attr(docsrs, doc(cfg(feature = "server")))] - fn register_explicit() -> Result<(), server_fn::ServerFnError> { - Self::register_in_explicit::() - } -} - -impl DioxusServerFn for T where T: server_fn::ServerFn<()> {} diff --git a/packages/fullstack/src/server_fn/mod.rs b/packages/fullstack/src/server_fn/mod.rs new file mode 100644 index 000000000..e2637b23b --- /dev/null +++ b/packages/fullstack/src/server_fn/mod.rs @@ -0,0 +1,24 @@ +#[cfg(feature = "server")] +pub(crate)mod collection; +#[cfg(feature = "server")] +pub mod service; + +/// Defines a "server function." A server function can be called from the server or the client, +/// but the body of its code will only be run on the server, i.e., if a crate feature `ssr` is enabled. +/// +/// Server functions are created using the `server` macro. +/// +/// The set of server functions +/// can be queried on the server for routing purposes by calling [server_fn::ServerFunctionRegistry::get]. +/// +/// Technically, the trait is implemented on a type that describes the server function's arguments, not the function itself. +pub trait DioxusServerFn: server_fn::ServerFn<()> { + /// Registers the server function, allowing the client to query it by URL. + #[cfg(feature = "server")] + #[cfg_attr(docsrs, doc(cfg(feature = "server")))] + fn register_explicit() -> Result<(), server_fn::ServerFnError> { + Self::register_in_explicit::() + } +} + +impl DioxusServerFn for T where T: server_fn::ServerFn<()> {} diff --git a/packages/fullstack/src/adapters/mod.rs b/packages/fullstack/src/server_fn/service.rs similarity index 84% rename from packages/fullstack/src/adapters/mod.rs rename to packages/fullstack/src/server_fn/service.rs index 3e8c9321f..22f1a176c 100644 --- a/packages/fullstack/src/adapters/mod.rs +++ b/packages/fullstack/src/server_fn/service.rs @@ -1,29 +1,19 @@ -//! # Adapters -//! Adapters for different web frameworks. -//! -//! Each adapter provides a set of utilities that is ergonomic to use with the framework. -//! -//! Each framework has utilies for some or all of the following: -//! - Server functions -//! - A generic way to register server functions -//! - A way to register server functions with a custom handler that allows users to pass in a custom [`crate::server_context::DioxusServerContext`] based on the state of the server framework. -//! - A way to register static WASM files that is accepts [`crate::serve_config::ServeConfig`] -//! - A hot reloading web socket that intigrates with [`dioxus-hot-reload`](https://crates.io/crates/dioxus-hot-reload) +//! # Server function Service -#[cfg(feature = "axum")] -pub mod axum_adapter; +//! This module defines a service that can be used to handle server functions. use http::StatusCode; use server_fn::{Encoding, Payload}; use std::sync::{Arc, RwLock}; -type MyBody = axum::body::Body; - +use crate::server_fn::collection::MIDDLEWARE; use crate::{ layer::{BoxedService, Service}, prelude::{DioxusServerContext, ProvideServerContext}, }; +type AxumBody = axum::body::Body; + /// Create a server function handler with the given server context and server function. pub fn server_fn_service( context: DioxusServerContext, @@ -31,7 +21,7 @@ pub fn server_fn_service( ) -> crate::layer::BoxedService { let prefix = function.prefix().to_string(); let url = function.url().to_string(); - if let Some(middleware) = crate::server_fn::MIDDLEWARE.get(&(&prefix, &url)) { + if let Some(middleware) = MIDDLEWARE.get(&(&prefix, &url)) { let mut service = BoxedService(Box::new(ServerFnHandler::new(context, function))); for middleware in middleware { service = middleware.layer(service); @@ -66,11 +56,11 @@ impl ServerFnHandler { impl Service for ServerFnHandler { fn run( &mut self, - req: http::Request, + req: http::Request, ) -> std::pin::Pin< Box< dyn std::future::Future< - Output = Result, server_fn::ServerFnError>, + Output = Result, server_fn::ServerFnError>, > + Send, >, > {