reorganize fullstack

This commit is contained in:
Evan Almloff 2024-02-16 09:03:35 -06:00
parent 2763adb2d3
commit 0ef29b7d75
7 changed files with 39 additions and 48 deletions

View file

@ -70,7 +70,7 @@ use std::sync::RwLock;
use crate::{ use crate::{
prelude::*, render::SSRState, serve_config::ServeConfig, server_context::DioxusServerContext, 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. /// A extension trait with utilities for integrating Dioxus with your Axum router.

View file

@ -124,7 +124,7 @@ impl Config {
#[cfg(feature = "axum")] #[cfg(feature = "axum")]
{ {
use crate::adapters::axum_adapter::{render_handler, DioxusRouterExt}; use crate::axum_adapter::{render_handler, DioxusRouterExt};
use axum::routing::get; use axum::routing::get;
use tower::ServiceBuilder; use tower::ServiceBuilder;

View file

@ -7,6 +7,7 @@ use dioxus_lib::prelude::{Element, VirtualDom};
pub use crate::Config; pub use crate::Config;
/// Launch a fullstack app with the given root component, contexts, and config. /// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch( pub fn launch(
root: fn() -> Element, root: fn() -> Element,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>, contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,

View file

@ -8,17 +8,13 @@ pub use once_cell;
mod html_storage; 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_attr(docsrs, doc(cfg(feature = "axum")))]
#[cfg(feature = "axum")] #[cfg(feature = "axum")]
pub use adapters::axum_adapter; mod axum_adapter;
#[cfg_attr(docsrs, doc(cfg(feature = "server")))] #[cfg_attr(docsrs, doc(cfg(feature = "server")))]
#[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 config;
mod hooks; mod hooks;
@ -48,7 +44,7 @@ pub mod prelude {
#[cfg(feature = "axum")] #[cfg(feature = "axum")]
#[cfg_attr(docsrs, doc(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(not(feature = "server"))]
#[cfg_attr(docsrs, doc(cfg(not(feature = "server"))))] #[cfg_attr(docsrs, doc(cfg(not(feature = "server"))))]
@ -87,7 +83,7 @@ pub mod prelude {
#[cfg(feature = "server")] #[cfg(feature = "server")]
#[cfg_attr(docsrs, doc(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::*; pub use dioxus_server_macro::*;
#[cfg(feature = "server")] #[cfg(feature = "server")]

View file

@ -171,23 +171,3 @@ pub enum ServerRegistrationFnError {
#[error("The server function registry is poisoned: {0}")] #[error("The server function registry is poisoned: {0}")]
Poisoned(String), 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::<DioxusServerFnRegistry>()
}
}
impl<T> DioxusServerFn for T where T: server_fn::ServerFn<()> {}

View file

@ -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::<crate::server_fn::collection::DioxusServerFnRegistry>()
}
}
impl<T> DioxusServerFn for T where T: server_fn::ServerFn<()> {}

View file

@ -1,29 +1,19 @@
//! # Adapters //! # Server function Service
//! 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)
#[cfg(feature = "axum")] //! This module defines a service that can be used to handle server functions.
pub mod axum_adapter;
use http::StatusCode; use http::StatusCode;
use server_fn::{Encoding, Payload}; use server_fn::{Encoding, Payload};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
type MyBody = axum::body::Body; use crate::server_fn::collection::MIDDLEWARE;
use crate::{ use crate::{
layer::{BoxedService, Service}, layer::{BoxedService, Service},
prelude::{DioxusServerContext, ProvideServerContext}, prelude::{DioxusServerContext, ProvideServerContext},
}; };
type AxumBody = axum::body::Body;
/// Create a server function handler with the given server context and server function. /// Create a server function handler with the given server context and server function.
pub fn server_fn_service( pub fn server_fn_service(
context: DioxusServerContext, context: DioxusServerContext,
@ -31,7 +21,7 @@ pub fn server_fn_service(
) -> crate::layer::BoxedService { ) -> crate::layer::BoxedService {
let prefix = function.prefix().to_string(); let prefix = function.prefix().to_string();
let url = function.url().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))); let mut service = BoxedService(Box::new(ServerFnHandler::new(context, function)));
for middleware in middleware { for middleware in middleware {
service = middleware.layer(service); service = middleware.layer(service);
@ -66,11 +56,11 @@ impl ServerFnHandler {
impl Service for ServerFnHandler { impl Service for ServerFnHandler {
fn run( fn run(
&mut self, &mut self,
req: http::Request<MyBody>, req: http::Request<AxumBody>,
) -> std::pin::Pin< ) -> std::pin::Pin<
Box< Box<
dyn std::future::Future< dyn std::future::Future<
Output = Result<http::Response<MyBody>, server_fn::ServerFnError>, Output = Result<http::Response<AxumBody>, server_fn::ServerFnError>,
> + Send, > + Send,
>, >,
> { > {