Module server

Source
Available on crate feature axum only.
Expand description

Dioxus utilities for the Axum server framework.

§Example

#![allow(non_snake_case)]
use dioxus::prelude::*;

fn main() {
    #[cfg(feature = "web")]
    // Hydrate the application on the client
    dioxus::launch(app);
    #[cfg(feature = "server")]
    {
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async move {
                // Get the address the server should run on. If the CLI is running, the CLI proxies fullstack into the main address
                // and we use the generated address the CLI gives us
                let address = dioxus::cli_config::fullstack_address_or_localhost();
                let listener = tokio::net::TcpListener::bind(address)
                    .await
                    .unwrap();
                axum::serve(
                        listener,
                        axum::Router::new()
                            // Server side render the application, serve static assets, and register server functions
                            .serve_dioxus_application(ServeConfigBuilder::default(), app)
                            .into_make_service(),
                    )
                    .await
                    .unwrap();
            });
     }
}

fn app() -> Element {
    let mut text = use_signal(|| "...".to_string());

    rsx! {
        button {
            onclick: move |_| async move {
                if let Ok(data) = get_server_data().await {
                    text.set(data);
                }
            },
            "Run a server function"
        }
        "Server said: {text}"
    }
}

#[server(GetServerData)]
async fn get_server_data() -> Result<String, ServerFnError> {
    Ok("Hello from the server!".to_string())
}

Modules§

launch
A launch function that creates an axum router for the LaunchBuilder

Structs§

RenderHandleState
State used by render_handler to render a dioxus component with axum

Traits§

DioxusRouterExt
A extension trait with utilities for integrating Dioxus with your Axum router.

Functions§

render_handler
SSR renderer handler for Axum with added context injection.