Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Create A Live Reload HTTP Terminal Client To Monitor axum Development In Rust

I'm working on my cauldron web server. I found this cool way to setup a terminal client that makes calls to it during development that shows responses. It's super handy. No more manually refreshing a browser to see changes.

The Code

Code

[package]
name = "axum_dev_watch"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = {version = "1", features = ["full"] }
axum = "0.6"

[dev-dependencies]
anyhow = "1"
httpc-test = "0.1.1"

Code

use anyhow::Result;

#[tokio::test]
async fn monitor() -> Result<()> {
    let client = httpc_test::new_client("http://localhost:8181")?;
    client.do_get("/").await?.print().await?;
    Ok(())
}

Code

use axum::response::IntoResponse;
use axum::routing::get;
use axum::{response::Html, Router};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    let index_route = Router::new().route("/", get(index_handler));
    let addr = SocketAddr::from(([127, 0, 0, 1], 8181));
    axum::Server::bind(&addr)
        .serve(index_route.into_make_service())
        .await
        .unwrap();
}

async fn index_handler() -> impl IntoResponse {
    println!("Got request for /");
    Html("Hello, world")
}

The Usage

Code

cargo watch -q -c -x "test -q monitor -- --nocapture"

Code

cargo watch -q -c -w src/ -x run

The Example

When the monitor process runs, it'll produce output like:

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


running 1 test

=== Response for GET http://localhost:8181/
=> Status         : 200 OK OK
=> Headers        :
   content-type: text/html; charset=utf-8
   content-length: 12
   date: Thu, 07 Sep 2023 22:50:48 GMT
=> Response Body  :
Hello, world
===


test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

References