Compare commits

..

2 Commits

Author SHA1 Message Date
d59949a8c1 [rustfmt] edition 2021 -> 2024 2024-12-01 16:52:16 +01:00
73e11d5596 [rust] edition 2024 2024-12-01 16:51:31 +01:00
4 changed files with 303 additions and 332 deletions

605
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,8 @@
[package] [package]
name = "expose-dir-via-http" name = "expose-dir-via-http"
version = "1.3.29" version = "1.1.13"
edition = "2024" edition = "2024"
rust-version = "1.85"
[lints.rust] [lints.rust]
unsafe_code = "forbid" unsafe_code = "forbid"

2
rust-toolchain.toml Normal file
View File

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

View File

@ -1,8 +1,6 @@
use std::{ use std::{
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
num::NonZeroUsize, num::NonZeroUsize,
path::PathBuf,
sync::Arc,
thread::available_parallelism, thread::available_parallelism,
}; };
@ -13,8 +11,8 @@ use clap::{Parser, crate_name, crate_version};
#[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),
) )