use std::time::Duration;
fn main() -> Result<(), serve_net::ServeError> {
let server = serve_net::http_behind_proxy("127.0.0.1:8080", "127.0.0.1, ::1", |req, reply| {
let client = req
.client()
.addr()
.map_or_else(|| "unknown".to_owned(), |a| a.to_string());
reply.text(
200,
&format!("you are {client}, asking for {}\n", req.path()),
);
})?;
if let Some(local) = server.local_addr() {
println!("listening on http://{local}/ behind a local proxy (press Enter to stop)");
}
let drain = server.drain_handle();
std::thread::spawn(move || {
let mut line = String::new();
let _ = std::io::stdin().read_line(&mut line);
drain.drain(Duration::from_secs(5));
});
let report = server.wait();
println!(
"stopped: {} graceful, {} killed",
report.graceful, report.killed
);
Ok(())
}