-
I would like to start a local Poem server and, when it's ready, open a webbrowser with something along the lines of the Currently, I have let app: Route = /* ... */;
Server::new(TcpListener::bind("127.0.0.1:8080")).run(app).await?; How do I schedule something like Also, if the port I'm listening too is busy, how do I try other ports? Is this what Update: For the second question, I realized that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
I have a theory of how to do this, based on the difference between an "Acceptor" and a "Listener". I'm not sure whether it's correct. I'll experiment if I don't hear from anybody. Here's the outline I'm thinking about. let mut port = 8000;
let acceptor = loop {
if port > max_port {
panic!();
}
// This seems like it never fails, unlike
// https://doc.rust-lang.org/nightly/std/net/struct.TcpListener.html#method.bind
let listener = TcpListener::bind(format!("127.0.0.1:{port}");
match listener.into_acceptor() {
Some(a) => return a,
Err(_) => { port += 1} // Could also check the ErrorKind of this error, perhaps?
}
}
println!("Server is ready for incoming connections at this point!");
println!("I'm hoping that, at this point, a browser connecting to \
127.0.0.1:{port} will wait for the server to start and process the request.");
start_web_browser();
Server::new(acceptor).run(app).await?; Update: https://docs.rs/poem/latest/src/poem/listener/tcp.rs.html#30-37 seems to confirm my theory. |
Beta Was this translation helpful? Give feedback.
I have a theory of how to do this, based on the difference between an "Acceptor" and a "Listener". I'm not sure whether it's correct. I'll experiment if I don't hear from anybody.
Here's the outline I'm thinking about.