Skip to content

Commit 4f4db3c

Browse files
committed
feat: provide api to customize server address (#5263)
Users who need to control the server address programmatically are forced to use std::env::set_var("PORT", ...), which is unsafe in Rust edition 2024. Adds serve_at(addr, cb) as a safe alternative. serve() now delegates to it internally.
1 parent 1766af4 commit 4f4db3c

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

examples/07-fullstack/custom_axum_serve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
//! `dioxus::serve` is most useful for customizing the server setup, such as adding middleware,
1212
//! custom routes, or integrating with existing axum backend code.
1313
//!
14-
//! Note that `dioxus::serve` is accepts a Router from `axum`. Dioxus will use the IP and PORT
14+
//! Note that `dioxus::serve` accepts a Router from `axum`. Dioxus will use the IP and PORT
1515
//! environment variables to determine where to bind the server. To customize the port, use environment
16-
//! variables or a `.env` file.
16+
//! variables, a `.env` file, or `dioxus::serve_at` to pass a `SocketAddr` directly.
1717
//!
1818
//! On other platforms (like desktop or mobile), you'll want to use `dioxus::launch` instead and then
1919
//! handle async loading of data through hooks like `use_future` or `use_resource` and give the user

packages/dioxus/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ pub use dioxus_server as server;
8888
#[cfg(feature = "server")]
8989
pub use dioxus_server::serve;
9090

91+
#[cfg(feature = "server")]
92+
pub use dioxus_server::serve_at;
93+
9194
#[cfg(feature = "devtools")]
9295
#[cfg_attr(docsrs, doc(cfg(feature = "devtools")))]
9396
pub use dioxus_devtools as devtools;
@@ -215,7 +218,7 @@ pub mod prelude {
215218
#[cfg(feature = "server")]
216219
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
217220
#[doc(inline)]
218-
pub use dioxus_server::{self, DioxusRouterExt, ServeConfig, ServerFunction, serve};
221+
pub use dioxus_server::{self, DioxusRouterExt, ServeConfig, ServerFunction, serve, serve_at};
219222

220223
#[cfg(feature = "router")]
221224
#[cfg_attr(docsrs, doc(cfg(feature = "router")))]

packages/fullstack-server/src/launch.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,41 @@ pub fn router(app: fn() -> Element) -> Router {
111111
/// The axum router will be bound to the address specified by the `IP` and `PORT` environment variables,
112112
/// defaulting to `127.0.0.1:8080` if not set.
113113
///
114+
/// To bind to a specific address, use [`serve_at`] instead.
115+
///
116+
/// This function uses axum to block on serving the application, and will not return.
117+
pub fn serve<F>(serve_it: impl FnMut() -> F) -> !
118+
where
119+
F: Future<Output = Result<Router, anyhow::Error>> + 'static,
120+
{
121+
serve_at(dioxus_cli_config::fullstack_address_or_localhost(), serve_it)
122+
}
123+
124+
/// Serve a fullstack dioxus application with a custom axum router, bound to the given address.
125+
///
126+
/// This is the same as [`serve`], but allows you to specify the address to bind to programmatically
127+
/// instead of relying on the `IP` and `PORT` environment variables.
128+
///
129+
/// # Example
130+
///
131+
/// ```rust,ignore
132+
/// use std::net::SocketAddr;
133+
///
134+
/// let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
135+
/// dioxus::serve_at(addr, || async {
136+
/// Ok(dioxus::server::router(app)
137+
/// .route("/health", axum::routing::get(|| async { "ok" })))
138+
/// });
139+
/// ```
140+
///
114141
/// This function uses axum to block on serving the application, and will not return.
115-
pub fn serve<F>(mut serve_it: impl FnMut() -> F) -> !
142+
pub fn serve_at<F>(addr: SocketAddr, mut serve_it: impl FnMut() -> F) -> !
116143
where
117144
F: Future<Output = Result<Router, anyhow::Error>> + 'static,
118145
{
119146
let cb = move || Box::pin(serve_it()) as _;
120147

121-
block_on(
122-
async move { serve_router(cb, dioxus_cli_config::fullstack_address_or_localhost()).await },
123-
);
148+
block_on(async move { serve_router(cb, addr).await });
124149

125150
unreachable!("Serving a fullstack app should never return")
126151
}

packages/fullstack-server/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub(crate) mod streaming;
3939

4040
pub use launch::router;
4141
pub use launch::serve;
42+
pub use launch::serve_at;
4243

4344
pub mod serverfn;
4445
pub use serverfn::*;

0 commit comments

Comments
 (0)