From 40c5108a1696a40f5e3d82cf19197612303752e1 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Thu, 23 May 2024 11:13:50 -0400 Subject: [PATCH] Allow changing binding port and address --- src/main.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 44b01a9..24bd451 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,11 +16,11 @@ use serde::Deserialize; use serde_json::Value; use std::ffi::OsStr; use std::ffi::OsString; +use std::net::ToSocketAddrs; use std::os::unix::prelude::OsStringExt; use std::path::PathBuf; use std::process::Stdio; use std::sync::Arc; -use std::{net::SocketAddr, str::FromStr}; use tokio::io::AsyncBufReadExt; use tokio::io::BufReader; use tokio::process::Command; @@ -36,6 +36,24 @@ use tracing::info; #[derive(Parser)] struct Args { + #[clap( + short = 'p', + long = "port", + env = "PORT", + default_value = "8888", + help = "Listen port" + )] + listen_port: u16, + + #[clap( + short = 'a', + long = "address", + env = "ADDRESS", + default_value = "127.0.0.1", + help = "Listen address" + )] + listen_address: String, + #[arg(default_values = ["--all"])] doc_args: Vec, } @@ -47,6 +65,8 @@ async fn main() -> Result<()> { .with_writer(std::io::stderr) .init(); + let args = Args::parse(); + info!("Getting standard docs path"); let std_docs_path = get_std_docs_path().await?; info!("Found: {std_docs_path:?}"); @@ -81,10 +101,12 @@ async fn main() -> Result<()> { .route("/local", get(add_slash)) .layer(TraceLayer::new_for_http().on_response(DefaultOnResponse::new().level(Level::INFO))); - let addr = SocketAddr::from_str("127.0.0.1:8888").unwrap(); - info!("Listening on address http://{}", addr); + let socket_addrs = + ToSocketAddrs::to_socket_addrs(&(args.listen_address.as_str(), args.listen_port))? + .collect::>(); + info!("Trying to listen on {socket_addrs:?}"); - axum::Server::bind(&addr) + axum::Server::bind(&socket_addrs[0]) .serve(app.into_make_service()) .await .map_err(Into::into)