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::{
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.

View file

@ -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;

View file

@ -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<dyn Fn() -> Box<dyn Any> + Send + Sync>>,

View file

@ -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")]

View file

@ -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::<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
//! 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<MyBody>,
req: http::Request<AxumBody>,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = Result<http::Response<MyBody>, server_fn::ServerFnError>,
Output = Result<http::Response<AxumBody>, server_fn::ServerFnError>,
> + Send,
>,
> {