Compare commits

..

No commits in common. "main" and "v1.0.41" have entirely different histories.

3 changed files with 287 additions and 510 deletions

764
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
[package] [package]
name = "expose-dir-via-http" name = "expose-dir-via-http"
version = "1.3.20" version = "1.0.41"
edition = "2024" edition = "2021"
[lints.rust] [lints.rust]
unsafe_code = "forbid" unsafe_code = "forbid"
@ -12,7 +12,7 @@ todo = "warn"
unwrap_used = "warn" unwrap_used = "warn"
[dependencies] [dependencies]
actix-web = { version = "4" } actix-web = { version = "4", features = ["experimental-io-uring"] }
actix-files = "0.6" actix-files = "0.6"
clap = { version = "4", features = ["cargo", "derive"] } clap = { version = "4", features = ["cargo", "derive"] }

View File

@ -1,20 +1,18 @@
use std::{ use std::{
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
num::NonZeroUsize, num::NonZeroUsize,
path::PathBuf,
sync::Arc,
thread::available_parallelism, thread::available_parallelism,
}; };
use actix_web::{App, HttpServer}; use actix_web::{App, HttpServer};
use clap::{Parser, crate_name, crate_version}; use clap::{crate_name, crate_version, Parser};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(name = crate_name!(), version = crate_version!())] #[clap(name = crate_name!(), version = crate_version!())]
struct Args { struct Args {
/// Directory to expose /// Directory to expose
#[clap(default_value = ".", value_parser = parse_valid_dir)] #[clap(default_value = ".")]
dir: PathBuf, dir: String,
/// IP address to use /// IP address to use
#[clap(default_value = "0.0.0.0")] #[clap(default_value = "0.0.0.0")]
@ -25,31 +23,18 @@ struct Args {
port: u16, port: u16,
} }
fn parse_valid_dir(dir: &str) -> Result<PathBuf, String> {
let path = PathBuf::from(dir);
match std::fs::metadata(&path) {
Ok(metadata) if metadata.is_dir() => Ok(path),
Ok(_) => Err(format!("{} is not a directory", path.display())),
Err(e) => Err(format!("Error accessing {}: {}", path.display(), e)),
}
}
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let args = Args::parse(); let args = Args::parse();
let dir = Arc::new(args.dir); let dir = args.dir;
let sock = SocketAddr::new(args.ip, args.port); let sock = SocketAddr::new(args.ip, args.port);
println!( println!("Starting HTTP server on {sock} exposing dir {dir}");
"Starting HTTP server on {sock} exposing dir {}",
dir.display()
);
HttpServer::new(move || { HttpServer::new(move || {
let dir = dir.clone();
App::new().service( App::new().service(
actix_files::Files::new("/", dir.as_ref()) actix_files::Files::new("/", dir.clone())
.show_files_listing() .show_files_listing()
.prefer_utf8(true), .prefer_utf8(true),
) )