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

Nested applications

workspace = { members = ["crates/events"] }

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

[dependencies]
axum = "0.8.8"
events = { version = "0.1.0", path = "crates/events" }
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, response::Html, routing::get};

mod users;

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>
    "#,
    )
}

fn create_router() -> Router {
    Router::new()
        .route("/", get(main_page))
        .nest("/user", users::create_router())
        .nest("/events", events::create_router())
}

#[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::{Router, extract::Path, response::Html, routing::get};

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

pub fn create_router() -> Router {
    Router::new().route("/{id}", get(user_page))
}
}
#![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()
}
}

the events crate

[package]
name = "events"
version = "0.1.0"
edition = "2024"

[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 events::create_router;

#[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::{Router, response::Html, routing::get};

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

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

pub fn create_router() -> Router {
    Router::new()
        .route("/future", get(future_events))
        .route("/past", get(past_events))
}
}
#![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_future_events() {
    let html = get_html("/future").await;
    assert_eq!(html, "Future events");
}

#[tokio::test]
async fn test_past_events() {
    let html = get_html("/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()
}
}