Added webserver and port number specification

This commit is contained in:
Random936 2024-09-22 20:08:41 -07:00
parent 1f7f636219
commit 7ec442c93b
5 changed files with 96 additions and 7 deletions

73
Cargo.lock generated
View File

@ -77,6 +77,12 @@ dependencies = [
"syn",
]
[[package]]
name = "autocfg"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "axum"
version = "0.7.6"
@ -147,6 +153,12 @@ dependencies = [
"windows-targets",
]
[[package]]
name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "bytes"
version = "1.7.2"
@ -381,6 +393,16 @@ version = "0.2.158"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
[[package]]
name = "lock_api"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.22"
@ -441,6 +463,29 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "parking_lot"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
]
[[package]]
name = "percent-encoding"
version = "2.3.1"
@ -497,6 +542,15 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4"
dependencies = [
"bitflags",
]
[[package]]
name = "rustc-demangle"
version = "0.1.24"
@ -515,6 +569,12 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.210"
@ -569,6 +629,15 @@ dependencies = [
"serde",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
dependencies = [
"libc",
]
[[package]]
name = "smallvec"
version = "1.13.2"
@ -621,9 +690,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998"
dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys",
@ -716,6 +788,7 @@ dependencies = [
"axum",
"clap",
"if-addrs",
"tokio",
]
[[package]]

View File

@ -9,3 +9,4 @@ edition = "2021"
axum = "0.7.6"
clap = { version = "4.5.18", features = ["derive"] }
if-addrs = "0.13.3"
tokio = { version = "1.40.0", features = ["full"] }

View File

@ -12,6 +12,10 @@ fn current_dir_as_string() -> String {
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
/// Port for the web server to listen on.
#[arg(short, long, default_value_t = 8000)]
pub port: u16,
/// Interface to print URLs for. Default is all interfaces.
#[arg(short, long, default_value_t = String::from("all"))]
pub interface: String,

View File

@ -1,8 +1,19 @@
use axum::Router;
use tokio::net::TcpListener;
use axum::routing::get;
mod argparse;
mod print_dir;
fn main() {
#[tokio::main]
async fn main() {
let args = argparse::parse_args();
println!("{:#?}", args);
print_dir::print_interface(&args.interface, &args.directory);
print_dir::print_interface(&args.interface, &args.port, &args.directory);
let app = Router::new().route("/", get(|| async { "Hello world!" }));
let listener = TcpListener::bind(format!("0.0.0.0:{}", args.port))
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}

View File

@ -3,7 +3,7 @@ use std::net::IpAddr;
use std::path::Path;
use if_addrs::{Interface, get_if_addrs};
fn print_single_interface<P: AsRef<Path>>(iface: Interface, path: P) -> io::Result<()> {
fn print_single_interface<P: AsRef<Path>>(iface: Interface, port: &u16, path: P) -> io::Result<()> {
for entry in fs::read_dir(path)? {
let entry = entry?;
let entry_path = entry.path();
@ -11,7 +11,7 @@ fn print_single_interface<P: AsRef<Path>>(iface: Interface, path: P) -> io::Resu
let filename = entry_path.file_name().unwrap();
println!("http://{}/{}", iface.ip(), filename.to_str().unwrap());
println!("http://{}:{}/{}", iface.ip(), port, filename.to_str().unwrap());
}
Ok(())
@ -24,7 +24,7 @@ fn is_last_octet_one(ip: IpAddr) -> bool {
}
}
pub fn print_interface<P: AsRef<Path>>(iface_str: &str, path: P) {
pub fn print_interface<P: AsRef<Path>>(iface_str: &str, port: &u16, path: P) {
for iface in get_if_addrs().unwrap() {
if iface_str == "all" {
if iface.is_loopback() || is_last_octet_one(iface.ip()) {
@ -34,6 +34,6 @@ pub fn print_interface<P: AsRef<Path>>(iface_str: &str, path: P) {
if iface_str != iface.name { continue }
}
print_single_interface(iface, &path).unwrap();
print_single_interface(iface, port, &path).unwrap();
}
}