document all features

This commit is contained in:
Evan Almloff committed 2024-02-09 12:15:59 -06:00
1 parent 374ff24141
commit b4d17abfc1
21 files changed
+110 -374

No files matched your search

+2 -1
View File
@@ -81,4 +81,5 @@ salvo = ["dep:salvo", "server", "http-body-util"]
server = ["server_fn/ssr", "dioxus_server_macro/server", "tokio", "tokio-util", "tokio-stream", "dioxus-ssr", "dioxus-ssr/incremental", "tower", "hyper", "http", "tower-layer", "anymap", "tracing-futures", "pin-project", "thiserror", "dioxus-cli-config"]
default-tls = ["server_fn/default-tls"]
rustls = ["server_fn/rustls"]
doc = ["thiserror"]
# This feature enables some nightly flags that make it more clear what structs/methods are available in each feature
nightly-doc = []
@@ -4,10 +4,10 @@
// ```
use axum_desktop::*;
use dioxus_fullstack::prelude::server_fn::set_server_url;
fn main() {
// Set the url of the server where server functions are hosted.
set_server_url("http://127.0.0.1:8080");
#[cfg(not(feature = "server"))]
dioxus::fullstack::prelude::server_fn::set_server_url("http://127.0.0.1:8080");
dioxus::desktop::launch(app)
}
@@ -4,7 +4,7 @@
// ```
use axum_desktop::*;
use dioxus_fullstack::prelude::*;
use dioxus::prelude::*;
#[tokio::main]
async fn main() {
@@ -27,7 +27,7 @@ async fn main() {
}
// Hydrate the page
#[cfg(feature = "web")]
#[cfg(all(feature = "web", not(feature = "server")))]
fn main() {
dioxus_web::launch_with_props(
dioxus_fullstack::router::RouteWithCfg::<Route>,
@@ -332,7 +332,7 @@ where
}
fn connect_hot_reload(self) -> Self {
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "server"))]
#[cfg(all(debug_assertions, feature = "hot-reload"))]
{
self.nest(
"/_dioxus",
@@ -354,7 +354,7 @@ where
.route("/hot_reload", get(hot_reload_handler)),
)
}
#[cfg(not(all(debug_assertions, feature = "hot-reload", feature = "server")))]
#[cfg(not(all(debug_assertions, feature = "hot-reload")))]
{
self
}
@@ -476,7 +476,7 @@ fn report_err<E: std::fmt::Display>(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 = "server"))]
#[cfg(all(debug_assertions, feature = "hot-reload"))]
pub async fn hot_reload_handler(ws: axum::extract::WebSocketUpgrade) -> impl IntoResponse {
use axum::extract::ws::Message;
use futures_util::StreamExt;
+2 -2
View File
@@ -12,8 +12,8 @@
#[cfg(feature = "axum")]
pub mod axum_adapter;
#[cfg(feature = "salvo")]
pub mod salvo_adapter;
// #[cfg(feature = "salvo")]
// pub mod salvo_adapter;
#[cfg(feature = "warp")]
pub mod warp_adapter;
@@ -291,17 +291,20 @@ impl DioxusRouterExt for Router {
) -> Self {
let cfg = cfg.into();
self.serve_static_assets(cfg.assets_path)
self.serve_static_assets(cfg.assets_path.clone())
.connect_hot_reload()
.register_server_fns(server_fn_path)
.push(Router::with_path("/<**any_path>").get(SSRHandler { cfg }))
.push(Router::with_path("/<**any_path>").get(SSRHandler {
config: cfg,
virtual_dom: virtual_dom_factory,
}))
}
fn connect_hot_reload(self) -> Self {
let mut _dioxus_router = Router::with_path("_dioxus");
_dioxus_router =
_dioxus_router.push(Router::with_path("hot_reload").handle(HotReloadHandler));
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "server"))]
#[cfg(all(debug_assertions, feature = "hot-reload"))]
{
_dioxus_router = _dioxus_router.push(Router::with_path("disconnect").handle(ignore_ws));
}
@@ -485,11 +488,11 @@ fn handle_error(error: impl Error + Send + Sync, res: &mut Response) {
}
/// 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(not(all(debug_assertions, feature = "hot-reload", feature = "server")))]
#[cfg(not(all(debug_assertions, feature = "hot-reload")))]
#[derive(Default)]
pub struct HotReloadHandler;
#[cfg(not(all(debug_assertions, feature = "hot-reload", feature = "server")))]
#[cfg(not(all(debug_assertions, feature = "hot-reload")))]
#[handler]
impl HotReloadHandler {
async fn handle(
@@ -503,11 +506,11 @@ impl HotReloadHandler {
}
/// 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 = "server"))]
#[cfg(all(debug_assertions, feature = "hot-reload"))]
#[derive(Default)]
pub struct HotReloadHandler;
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "server"))]
#[cfg(all(debug_assertions, feature = "hot-reload"))]
#[handler]
impl HotReloadHandler {
async fn handle(
@@ -561,7 +564,7 @@ impl HotReloadHandler {
}
}
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "server"))]
#[cfg(all(debug_assertions, feature = "hot-reload"))]
#[handler]
async fn ignore_ws(req: &mut Request, res: &mut Response) -> Result<(), salvo::http::StatusError> {
use salvo::websocket::WebSocketUpgrade;
@@ -187,7 +187,7 @@ pub fn serve_dioxus_application(
) -> BoxedFilter<(impl Reply,)> {
let cfg = cfg.into();
// Serve the dist folder and the index.html file
let serve_dir = warp::fs::dir(cfg.assets_path);
let serve_dir = warp::fs::dir(cfg.assets_path.clone());
let virtual_dom_factory =
Arc::new(virtual_dom_factory) as Arc<dyn Fn() -> VirtualDom + Send + Sync + 'static>;
@@ -332,13 +332,13 @@ impl warp::reject::Reject for RecieveFailed {}
/// ```
pub fn connect_hot_reload() -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone
{
#[cfg(not(all(debug_assertions, feature = "hot-reload", feature = "server")))]
#[cfg(not(all(debug_assertions, feature = "hot-reload")))]
{
warp::path!("_dioxus" / "hot_reload")
.map(warp::reply)
.map(|reply| warp::reply::with_status(reply, warp::http::StatusCode::NOT_FOUND))
}
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "server"))]
#[cfg(all(debug_assertions, feature = "hot-reload"))]
{
use crate::hot_reload::HotReloadState;
use futures_util::sink::SinkExt;
@@ -349,7 +349,7 @@ pub fn connect_hot_reload() -> impl Filter<Extract = (impl Reply,), Error = warp
.and(warp::any().then(crate::hot_reload::spawn_hot_reload))
.and(warp::ws())
.map(move |state: &'static HotReloadState, ws: warp::ws::Ws| {
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "server"))]
// #[cfg(all(debug_assertions, feature = "hot-reload"))]
ws.on_upgrade(move |mut websocket| {
async move {
println!("🔥 Hot Reload WebSocket connected");
@@ -393,7 +393,7 @@ pub fn connect_hot_reload() -> impl Filter<Extract = (impl Reply,), Error = warp
.and(warp::ws())
.map(move |ws: warp::ws::Ws| {
println!("disconnect");
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "server"))]
#[cfg(all(debug_assertions, feature = "hot-reload",))]
ws.on_upgrade(move |mut websocket| async move {
struct DisconnectOnDrop(Option<warp::ws::WebSocket>);
impl Drop for DisconnectOnDrop {
+8
View File
@@ -53,6 +53,7 @@ impl Config {
/// Set the address to serve the app on.
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub fn addr(self, addr: impl Into<std::net::SocketAddr>) -> Self {
let addr = addr.into();
Self { addr, ..self }
@@ -60,6 +61,7 @@ impl Config {
/// Set the route to the server functions.
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub fn server_fn_route(self, server_fn_route: &'static str) -> Self {
Self {
server_fn_route,
@@ -69,6 +71,7 @@ impl Config {
/// Set the incremental renderer config.
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub fn incremental(self, cfg: IncrementalRendererConfig) -> Self {
Self {
server_cfg: self.server_cfg.incremental(cfg),
@@ -78,18 +81,21 @@ impl Config {
/// Set the server config.
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub fn server_cfg(self, server_cfg: ServeConfigBuilder) -> Self {
Self { server_cfg, ..self }
}
/// Set the web config.
#[cfg(feature = "web")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "web")))]
pub fn web_cfg(self, web_cfg: dioxus_web::Config) -> Self {
Self { web_cfg, ..self }
}
/// Set the desktop config.
#[cfg(feature = "desktop")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "desktop")))]
pub fn desktop_cfg(self, desktop_cfg: dioxus_desktop::Config) -> Self {
Self {
desktop_cfg,
@@ -99,11 +105,13 @@ impl Config {
/// Set the mobile config.
#[cfg(feature = "mobile")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "mobile")))]
pub fn mobile_cfg(self, mobile_cfg: dioxus_mobile::Config) -> Self {
Self { mobile_cfg, ..self }
}
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
/// Launch a server application
pub async fn launch_server(
self,
+56 -6
View File
@@ -2,6 +2,7 @@
#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
#![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
#![deny(missing_docs)]
#![cfg_attr(any(docsrs, feature = "nightly-doc"), feature(doc_cfg))]
pub use once_cell;
@@ -9,8 +10,20 @@ mod html_storage;
#[cfg(feature = "server")]
mod adapters;
// Splitting up the glob export lets us document features required for each adapter
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "axum")))]
#[cfg(feature = "axum")]
pub use adapters::axum_adapter;
// TODO: Compilation seems to be broken with the salvo feature enabled. Fix and add more features to checks in CI
// #[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "salvo")))]
// #[cfg(feature = "salvo")]
// pub use adapters::salvo_adapter;
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "warp")))]
#[cfg(feature = "warp")]
pub use adapters::warp_adapter;
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
#[cfg(feature = "server")]
pub use adapters::*;
pub use adapters::{server_fn_service, ServerFnHandler};
mod config;
mod hooks;
#[cfg(all(debug_assertions, feature = "hot-reload", feature = "server"))]
@@ -30,35 +43,57 @@ mod server_fn;
/// A prelude of commonly used items in dioxus-fullstack.
pub mod prelude {
#[cfg(feature = "axum")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "axum")))]
pub use crate::adapters::axum_adapter::*;
#[cfg(feature = "salvo")]
pub use crate::adapters::salvo_adapter::*;
// #[cfg(feature = "salvo")]
// #[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "salvo")))]
// pub use crate::adapters::salvo_adapter::*;
#[cfg(feature = "warp")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "warp")))]
pub use crate::adapters::warp_adapter::*;
use crate::hooks;
#[cfg(not(feature = "server"))]
#[cfg_attr(
any(docsrs, feature = "nightly-doc"),
doc(cfg(not(feature = "server")))
)]
pub use crate::html_storage::deserialize::get_root_props_from_document;
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub use crate::layer::{Layer, Service};
#[cfg(all(feature = "server", feature = "router"))]
#[cfg_attr(
any(docsrs, feature = "nightly-doc"),
doc(cfg(all(feature = "server", feature = "router")))
)]
pub use crate::render::pre_cache_static_routes_with_props;
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub use crate::render::SSRState;
#[cfg(feature = "router")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "router")))]
pub use crate::router::FullstackRouterConfig;
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub use crate::serve_config::{ServeConfig, ServeConfigBuilder};
#[cfg(all(feature = "server", feature = "axum"))]
#[cfg_attr(
any(docsrs, feature = "nightly-doc"),
doc(cfg(all(feature = "server", feature = "axum")))
)]
pub use crate::server_context::Axum;
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub use crate::server_context::{
extract, server_context, DioxusServerContext, FromServerContext, ProvideServerContext,
};
pub use crate::server_fn::DioxusServerFn;
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub use crate::server_fn::{ServerFnMiddleware, ServerFnTraitObj, ServerFunction};
pub use dioxus_server_macro::*;
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
pub use dioxus_ssr::incremental::IncrementalRendererConfig;
pub use server_fn::{self, ServerFn as _, ServerFnError};
@@ -66,11 +101,26 @@ pub mod prelude {
}
// Warn users about overlapping features
#[cfg(all(feature = "server", feature = "web", not(doc)))]
#[cfg(all(
feature = "server",
feature = "web",
not(doc),
not(feature = "nightly-doc")
))]
compile_error!("The `ssr` feature (enabled by `warp`, `axum`, or `salvo`) and `web` feature are overlapping. Please choose one or the other.");
#[cfg(all(feature = "server", feature = "desktop", not(doc)))]
#[cfg(all(
feature = "server",
feature = "desktop",
not(doc),
not(feature = "nightly-doc")
))]
compile_error!("The `ssr` feature (enabled by `warp`, `axum`, or `salvo`) and `desktop` feature are overlapping. Please choose one or the other.");
#[cfg(all(feature = "server", feature = "mobile", not(doc)))]
#[cfg(all(
feature = "server",
feature = "mobile",
not(doc),
not(feature = "nightly-doc")
))]
compile_error!("The `ssr` feature (enabled by `warp`, `axum`, or `salvo`) and `mobile` feature are overlapping. Please choose one or the other.");
+1
View File
@@ -228,6 +228,7 @@ impl<T: Send + Sync + Clone + 'static> FromServerContext for FromContext<T> {
}
#[cfg(feature = "axum")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "axum")))]
/// An adapter for axum extractors for the server context
pub struct Axum;
+14 -8
View File
@@ -1,9 +1,10 @@
#[cfg(any(feature = "server", doc))]
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
#[derive(Clone)]
/// A trait object for a function that be called on serializable arguments and returns a serializable result.
pub struct ServerFnTraitObj(server_fn::ServerFnTraitObj<()>);
#[cfg(any(feature = "server", doc))]
#[cfg(feature = "server")]
impl std::ops::Deref for ServerFnTraitObj {
type Target = server_fn::ServerFnTraitObj<()>;
@@ -12,14 +13,14 @@ impl std::ops::Deref for ServerFnTraitObj {
}
}
#[cfg(any(feature = "server", doc))]
#[cfg(feature = "server")]
impl std::ops::DerefMut for ServerFnTraitObj {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[cfg(any(feature = "server", doc))]
#[cfg(feature = "server")]
impl ServerFnTraitObj {
fn new(
prefix: &'static str,
@@ -40,6 +41,7 @@ impl ServerFnTraitObj {
server_fn::inventory::collect!(ServerFnTraitObj);
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
/// Middleware for a server function
pub struct ServerFnMiddleware {
/// The prefix of the server function.
@@ -72,7 +74,8 @@ pub(crate) static MIDDLEWARE: once_cell::sync::Lazy<
#[cfg(feature = "server")]
server_fn::inventory::collect!(ServerFnMiddleware);
#[cfg(any(feature = "server", doc))]
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
/// A server function that can be called on serializable arguments and returns a serializable result.
pub type ServerFunction = server_fn::SerializedFnTraitObj<()>;
@@ -88,7 +91,8 @@ static REGISTERED_SERVER_FUNCTIONS: once_cell::sync::Lazy<
std::sync::Arc::new(std::sync::RwLock::new(map))
});
#[cfg(any(feature = "server", doc))]
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
/// The registry of all Dioxus server functions.
pub struct DioxusServerFnRegistry;
@@ -155,7 +159,8 @@ impl server_fn::ServerFunctionRegistry<()> for DioxusServerFnRegistry {
}
}
#[cfg(any(feature = "server", doc))]
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
/// Errors that can occur when registering a server function.
#[derive(thiserror::Error, Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum ServerRegistrationFnError {
@@ -178,7 +183,8 @@ pub enum ServerRegistrationFnError {
/// 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(any(feature = "server", doc))]
#[cfg(feature = "server")]
#[cfg_attr(any(docsrs, feature = "nightly-doc"), doc(cfg(feature = "server")))]
fn register_explicit() -> Result<(), server_fn::ServerFnError> {
Self::register_in_explicit::<DioxusServerFnRegistry>()
}