Files
dioxus/examples/07-fullstack/middleware.rs
T
Evan AlmloffandJonathan Kelley c977cc125e Switch const serialize to use CBOR (#4932)
* implement const cbor encoding + decoding for basic types

* implement map encoding and decoding

* fix map test

* all tests passing

* dynamically sized arrays

* smaller str encoding

* swap array and list and add more documentation

* move serialization and deserialization for each struct together

* support both legacy and new assets

* bump const serialize version

* fix asset conversion code

* pull out manganis symbol struct from tuble

* test reading old asset versions

* more list/array cleanup and bump rust version

* fix clippy

* switch cli opt port

* install playwright deps

* split cli-optimization package to avoid two dioxus' in the same crate

* fix clippy

* force the toast.png asset to be included in the linker stage #[used] has platform depedant behavior

* bundle both formats in the link section and only use the asset the cli changed

* remove sudo which doesn't exist on windows

* add binstall to the playwright workflow

* separate bin target for the old cli

* lockfile

* cargo update to get versions shaken out

* notes on binstalls, add back `.read()` for compat

* use 0.8.0-alpha.0 for const-serialize instead of 0.8

this ensures we can still publish breaking changes on 0.8 without taking over the full version

* fix clippy in example

* small debug nits

* update rust-overlay in nix

* fix flake?

* migrate to new structure

* just comment out binstalls

* just remove old test harness

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
2025-12-28 21:49:34 -05:00

56 lines
1.9 KiB
Rust

//! This example shows how to use middleware in a fullstack Dioxus app.
//!
//! Dioxus supports two ways of middleware:
//! - Applying layers to the top-level axum router
//! - Apply `#[middleware]` attributes to individual handlers
use dioxus::prelude::*;
#[cfg(feature = "server")]
use {std::time::Duration, tower_http::timeout::TimeoutLayer};
fn main() {
#[cfg(not(feature = "server"))]
dioxus::launch(app);
#[cfg(feature = "server")]
dioxus::serve(|| async move {
use axum::{extract::Request, middleware::Next};
use dioxus::server::axum;
Ok(dioxus::server::router(app)
// we can apply a layer to the entire router using axum's `.layer` method
.layer(axum::middleware::from_fn(
|request: Request, next: Next| async move {
println!("Request: {} {}", request.method(), request.uri().path());
let res = next.run(request).await;
println!("Response: {}", res.status());
res
},
)))
});
}
fn app() -> Element {
let mut per_route = use_action(per_route_middleware);
rsx! {
h1 { "Fullstack Middleware Example" }
button { onclick: move |_| per_route.call(), "Fetch Data" }
pre { "{per_route.value():#?}" }
}
}
// We can use the `#[middleware]` attribute to apply middleware to individual handlers.
//
// Here, we're applying a timeout to the `per_route_middleware` handler, which will return a 504
// if the handler takes longer than 3 seconds to complete.
//
// To add multiple middleware layers, simply stack multiple `#[middleware]` attributes.
#[get("/api/count")]
#[middleware(TimeoutLayer::with_status_code(408.try_into().unwrap(), Duration::from_secs(3)))]
async fn per_route_middleware() -> Result<String> {
tokio::time::sleep(Duration::from_secs(5)).await;
Ok("Hello, world!".to_string())
}