Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Nesting applications

In this examples we have 3 different routes, /events/future, /events/past and /user/ID where ID can be any number. Effectively there are 2 applications the /events/ application and the /user/ application.

Here we implemented separate functions for each one of them, all in the same crate.

[package]
name = "nesting-applications"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
axum = "0.8.8"
tokio = { version = "1.50.0", features = ["full"] }

[dev-dependencies]
http-body-util = "0.1.3"
tower = { version = "0.5.3", features = ["util"] }
use axum::{Router, extract::Path, response::Html, routing::get};

async fn main_page() -> Html<&'static str> {
    Html(
        r#"
    <a href="/events/future">/events/future</a><br>
    <a href="/events/past">/events/past</a><br>
    <a href="/user/42">/user/42</a><br>
    "#,
    )
}

async fn future_events() -> Html<String> {
    Html(String::from("Future events"))
}

async fn past_events() -> Html<String> {
    Html(String::from("Past events"))
}

async fn user_page(Path(id): Path<u32>) -> Html<String> {
    Html(format!("User id: {}", id))
}

fn create_router() -> Router {
    Router::new()
        .route("/", get(main_page))
        .route("/user/{id}", get(user_page))
        .route("/events/future", get(future_events))
        .route("/events/past", get(past_events))
}

#[tokio::main]
async fn main() {
    let app = create_router();

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    println!("listening on http://{}", listener.local_addr().unwrap());
    axum::serve(listener, app).await.unwrap();
}

#[cfg(test)]
mod tests;
#![allow(unused)]
fn main() {
use axum::{body::Body, http::Request, http::StatusCode};
use http_body_util::BodyExt;
use tower::ServiceExt;

use super::*;

#[tokio::test]
async fn test_main_page() {
    let html = get_html("/").await;
    assert!(html.contains(r#"<a href="/user/42">/user/42</a>"#));
}

#[tokio::test]
async fn test_user_page() {
    let html = get_html("/user/42").await;
    assert_eq!(html, "User id: 42");
}

#[tokio::test]
async fn test_future_events() {
    let html = get_html("/events/future").await;
    assert_eq!(html, "Future events");
}

#[tokio::test]
async fn test_past_events() {
    let html = get_html("/events/past").await;
    assert_eq!(html, "Past events");
}

async fn get_html(uri: &str) -> String {
    let response = create_router()
        .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let body = response.into_body();
    let bytes = body.collect().await.unwrap().to_bytes();
    String::from_utf8(bytes.to_vec()).unwrap()
}
}