Allow changing binding port and address

This commit is contained in:
Austen Adler 2024-05-23 11:13:50 -04:00
parent 69062c4c39
commit 40c5108a16

View File

@ -16,11 +16,11 @@ use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::ffi::OsString; use std::ffi::OsString;
use std::net::ToSocketAddrs;
use std::os::unix::prelude::OsStringExt; use std::os::unix::prelude::OsStringExt;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Stdio; use std::process::Stdio;
use std::sync::Arc; use std::sync::Arc;
use std::{net::SocketAddr, str::FromStr};
use tokio::io::AsyncBufReadExt; use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader; use tokio::io::BufReader;
use tokio::process::Command; use tokio::process::Command;
@ -36,6 +36,24 @@ use tracing::info;
#[derive(Parser)] #[derive(Parser)]
struct Args { 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"])] #[arg(default_values = ["--all"])]
doc_args: Vec<String>, doc_args: Vec<String>,
} }
@ -47,6 +65,8 @@ async fn main() -> Result<()> {
.with_writer(std::io::stderr) .with_writer(std::io::stderr)
.init(); .init();
let args = Args::parse();
info!("Getting standard docs path"); info!("Getting standard docs path");
let std_docs_path = get_std_docs_path().await?; let std_docs_path = get_std_docs_path().await?;
info!("Found: {std_docs_path:?}"); info!("Found: {std_docs_path:?}");
@ -81,10 +101,12 @@ async fn main() -> Result<()> {
.route("/local", get(add_slash)) .route("/local", get(add_slash))
.layer(TraceLayer::new_for_http().on_response(DefaultOnResponse::new().level(Level::INFO))); .layer(TraceLayer::new_for_http().on_response(DefaultOnResponse::new().level(Level::INFO)));
let addr = SocketAddr::from_str("127.0.0.1:8888").unwrap(); let socket_addrs =
info!("Listening on address http://{}", addr); ToSocketAddrs::to_socket_addrs(&(args.listen_address.as_str(), args.listen_port))?
.collect::<Vec<_>>();
info!("Trying to listen on {socket_addrs:?}");
axum::Server::bind(&addr) axum::Server::bind(&socket_addrs[0])
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .await
.map_err(Into::into) .map_err(Into::into)