-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Description
Heya 👋, I ran into something unexpected the other day with the Server.local_addr()
method. I was expecting to be able to use it if Server::Builder
was called using an AddrIncoming
as a type parameter - but apparently it's not available. This isn't a major thing, but it adds a few extra lines of boilerplate if you want to print the address the server is listening on (see below).
I'm don't know if this was designed to be this way or is a bug of any sorts. But I reckon it'd be great if we could make this work! -- If you think this is a good idea to help fix, I'd be happy to submit any patches needed to make this work! -- Thanks heaps! ✨
Examples
Expected
use futures::prelude::*;
use http_03::cli::Cli;
use hyper::{service::service_fn_ok, Body, Response, Server};
use structopt::StructOpt;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Cli::from_args();
args.logger.log_all(args.verbosity.log_level())?;
let server = Server::from_tcp(args.port.bind()?)? // Listener is initialized inline
.serve(|| service_fn_ok(|_| Response::new(Body::from("Hello World"))))
.map_err(|err| error!("server error {}", err));
info!("listening on {}", server.local_addr()); // `addr` is available on the server
tokio::run(server);
Ok(())
}
Current
use futures::prelude::*;
use http_03::cli::Cli;
use hyper::{service::service_fn_ok, Body, Response, Server};
use structopt::StructOpt;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Cli::from_args();
args.logger.log_all(args.verbosity.log_level())?;
let listener = args.port.bind()?; // This can't be initialized inline
let addr = listener.local_addr()?; // Because we need to extract the `addr` here
let server = Server::from_tcp(listener)?
.serve(|| service_fn_ok(|_| Response::new(Body::from("Hello World"))))
.map_err(|err| error!("server error {}", err));
info!("listening on {}", addr); // Uses the `addr` from above
tokio::run(server);
Ok(())
}
Metadata
Metadata
Assignees
Labels
No labels