When attempting to use axum::serve on axum version 0.7.5, or axum::Server::serve on verison 0.6, my code compiles without errors, but leaves me with an infinite runtime and no way to continue using the Command line interface to run commands like curl
or netstat
.
Here is my code in my serve.rs
file:
pub fn configure() -> Command {...} // configures serve command
pub fn handle(matches: &ArgMatches, _settings: &Settings) -> anyhow::Result<()> {
if let Some(matches) = matches.subcommand_matches("serve") {
let port: u16 = *matches.get_one("port").unwrap_or(&8080);
start_tokio(port, _settings)?;
}
Ok(())
}
fn start_tokio(port: u16, _settings: &Settings) -> anyhow::Result<()> { // creates a new tokio multi-threaded runtime, then start an axum server on 0.0.0.0:<port>
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async move {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), port);
let routes = crate::api::configure();
// Checkpoint 1 - executes
let listener = TcpListener::bind(addr).await.unwrap();
axum::serve(listener, routes.into_make_service()).await.unwrap();
// Checkpoint 2 - doesn't execute
Ok::<(), anyhow::Error>(())
})?;
std::process::exit(0);
}
In start_tokio
, any code written at “checkpoint 1” will execute just fine, but anything after the axum::serve statement will never run, and it seems like the program is stuck running in a loop on that line because the CLI will remain empty after I run ./target/debug/project_directory serve
, which brings me back to the main issue of not being able to run curl
or netstat
commands.
I am following a guide: https://apatisandor.hu/blog/production-ready-axum/ (updating the code a little bit to accomodate for newer versions), and there he is able to run those without issue, so maybe I’m just doing something wrong that I didn’t understand or is the guide outdated and it doesn’t work like that anymore?