[feat] only allow valid dirs as 1st arg
This commit is contained in:
parent
05142358e1
commit
30874a97f7
19
src/main.rs
19
src/main.rs
@ -1,6 +1,7 @@
|
||||
use std::{
|
||||
net::{IpAddr, SocketAddr},
|
||||
num::NonZeroUsize,
|
||||
path::PathBuf,
|
||||
thread::available_parallelism,
|
||||
};
|
||||
|
||||
@ -11,8 +12,8 @@ use clap::{crate_name, crate_version, Parser};
|
||||
#[clap(name = crate_name!(), version = crate_version!())]
|
||||
struct Args {
|
||||
/// Directory to expose
|
||||
#[clap(default_value = ".")]
|
||||
dir: String,
|
||||
#[clap(default_value = ".", value_parser = parse_valid_dir)]
|
||||
dir: PathBuf,
|
||||
|
||||
/// IP address to use
|
||||
#[clap(default_value = "0.0.0.0")]
|
||||
@ -23,6 +24,15 @@ struct Args {
|
||||
port: u16,
|
||||
}
|
||||
|
||||
fn parse_valid_dir(dir: &str) -> Result<PathBuf, String> {
|
||||
let path = std::path::Path::new(dir);
|
||||
if path.is_dir() {
|
||||
Ok(path.to_path_buf())
|
||||
} else {
|
||||
Err(format!("{} is not a valid directory", path.display()))
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let args = Args::parse();
|
||||
@ -30,7 +40,10 @@ async fn main() -> std::io::Result<()> {
|
||||
let dir = args.dir;
|
||||
let sock = SocketAddr::new(args.ip, args.port);
|
||||
|
||||
println!("Starting HTTP server on {sock} exposing dir {dir}");
|
||||
println!(
|
||||
"Starting HTTP server on {sock} exposing dir {}",
|
||||
dir.display()
|
||||
);
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new().service(
|
||||
|
Loading…
x
Reference in New Issue
Block a user