Implement clap for command line argument parsing, and cleanup of path traversal logic, now readable
All checks were successful
Compile to Binary / build-linux (push) Successful in 57s
Compile to Binary / build-linux-musl (push) Successful in 1m14s
Compile to Binary / build-windows (push) Successful in 1m38s

This commit is contained in:
2026-03-18 09:52:13 +01:00
parent b6e8ae4ca8
commit a025ca2ec4
4 changed files with 49 additions and 23 deletions

View File

@@ -6,25 +6,45 @@ use std::path::PathBuf;
use std::sync::Arc;
use logger_rust::*;
use clap::Parser;
// 1CA:b6e8ae4ca8acae66aa3dfd3f0a377e91d041e4d7 ; added clap as a parser, its way cleaner
#[derive(Parser, Debug)]
#[command(name = "http-rs")]
#[command(about = "http-rs webserver")]
struct Arguments {
/// port where we should serve on
#[arg(short, long, default_value_t = 8080)]
port : u16,
/// where we should serve from
#[arg(short, long, default_value = ".")]
root: PathBuf,
/// number of threads to run on
#[arg(short, long)]
threads: Option<usize>,
}
fn main() {
let args: Vec<String> = std::env::args().collect();
let port: u16 = args.get(1)
.and_then(|p| p.parse().ok())
.unwrap_or(8080);
let root = Arc::new(
PathBuf::from(args.get(2).map(|s| s.as_str()).unwrap_or("."))
);
let args = Arguments::parse();
let root = Arc::new(args.root);
if !root.exists() || !root.is_dir() {
log_error!("Root {} isnt a valid directory", root.display());
std::process::exit(1);
}
// moved num_cpus from server.rs to main.rs
let threads = args.threads.unwrap_or_else(|| {
std::thread::available_parallelism()
.map(|n| n.get() * 2)
.unwrap_or(8)
});
if let Err(_) = server::run(root, port) {
// shit out error if we have port already in use,
// prevents panic
log_error!("Port {} already in use!", port);
if let Err(_) = server::run(root, args.port, threads) {
log_error!("port {} in use!", args.port);
}
}