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

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();
}
}