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 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<String>,
}
@ -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::<Vec<_>>();
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)