I just finished porting a web server application I had working in actix-web over to axum.
Things went pretty smoothly until I ran into a problem using the daemonize crate so that my web server could run as a background (i.e. daemon) process.
Using daemonize and coding it the way I did with actix-web, the server would start with no errors, but all http client requests. i.e.:
curl http://localhost:3000
would just hang.
I found that I had to create a tokio::Runtime instance manually from within the daemonize wrapper, instead of being able to use the tokio::main attribute.
Any ideas why this is the case?
Here's my Cargo.toml:
[package]
name = "example-hello-world"
version = "0.1.0"
edition = "2018"
publish = false
[dependencies]
axum = "0.4.5"
tokio = { version = "1.0", features = ["full"] }
daemonize = "0.4.1"
Here's the code that I should work but requests hang. It's just the axim hello-world example wrapped in daemonize.
use axum::{response::Html, routing::get, Router};
use std::net::SocketAddr;
use std::fs::File;
use daemonize::Daemonize;
#[tokio::main]
async fn main() {
let stdout = File::create("/tmp/daemon.out").unwrap();
let stderr = File::create("/tmp/daemon.err").unwrap();
let daemonize = Daemonize::new()
.pid_file("/tmp/test.pid") // Every method except `new` and `start`
.working_directory("/tmp") // for default behaviour.
.stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
.stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
.privileged_action(|| "Executed before drop privileges");
match daemonize.start() {
Ok(_) => {
// build our application with a route
let app = Router::new().route("/", get(handler));
// run it
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
println!("Success, daemonized");
},
Err(e) => eprintln!("Error, {}", e),
}
}
async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}
Here's the code I got to work using a manual tokio::Runtime, but I'd rather not have to do it this way because it requires a lot of code refactoring.
use axum::{response::Html, routing::get, Router};
use std::net::SocketAddr;
use std::fs::File;
use daemonize::Daemonize;
use tokio::runtime::Runtime;
fn main() {
let stdout = File::create("/tmp/daemon.out").unwrap();
let stderr = File::create("/tmp/daemon.err").unwrap();
let daemonize = Daemonize::new()
.pid_file("/tmp/test.pid") // Every method except `new` and `start`
.working_directory("/tmp") // for default behaviour.
.stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
.stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
.privileged_action(|| "Executed before drop privileges");
match daemonize.start() {
Ok(_) => {
let rt = Runtime::new().unwrap();
rt.block_on(async {
// build our application with a route
let app = Router::new().route("/", get(handler));
// run it
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
println!("Success, daemonized");
});
},
Err(e) => eprintln!("Error, {}", e),
}
}
async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}