Compare commits
No commits in common. "main" and "v1.0.0" have entirely different histories.
1046
Cargo.lock
generated
1046
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
14
Cargo.toml
@ -1,18 +1,10 @@
|
||||
[package]
|
||||
name = "expose-dir-via-http"
|
||||
version = "1.3.20"
|
||||
edition = "2024"
|
||||
|
||||
[lints.rust]
|
||||
unsafe_code = "forbid"
|
||||
|
||||
[lints.clippy]
|
||||
pedantic = { level = "warn", priority = -1 }
|
||||
todo = "warn"
|
||||
unwrap_used = "warn"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { version = "4" }
|
||||
actix-web = { version = "4", features = ["experimental-io-uring"] }
|
||||
actix-files = "0.6"
|
||||
clap = { version = "4", features = ["cargo", "derive"] }
|
||||
|
||||
|
31
src/main.rs
31
src/main.rs
@ -1,20 +1,18 @@
|
||||
use std::{
|
||||
env,
|
||||
net::{IpAddr, SocketAddr},
|
||||
num::NonZeroUsize,
|
||||
path::PathBuf,
|
||||
sync::Arc,
|
||||
thread::available_parallelism,
|
||||
};
|
||||
|
||||
use actix_web::{App, HttpServer};
|
||||
use clap::{Parser, crate_name, crate_version};
|
||||
use clap::{crate_name, crate_version, Parser};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(name = crate_name!(), version = crate_version!())]
|
||||
struct Args {
|
||||
/// Directory to expose
|
||||
#[clap(default_value = ".", value_parser = parse_valid_dir)]
|
||||
dir: PathBuf,
|
||||
#[clap(default_value = ".")]
|
||||
dir: String,
|
||||
|
||||
/// IP address to use
|
||||
#[clap(default_value = "0.0.0.0")]
|
||||
@ -25,36 +23,23 @@ struct Args {
|
||||
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]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
let dir = Arc::new(args.dir);
|
||||
let dir = args.dir;
|
||||
let sock = SocketAddr::new(args.ip, args.port);
|
||||
|
||||
println!(
|
||||
"Starting HTTP server on {sock} exposing dir {}",
|
||||
dir.display()
|
||||
);
|
||||
println!("Starting HTTP server on {sock} exposing dir {dir}");
|
||||
|
||||
HttpServer::new(move || {
|
||||
let dir = dir.clone();
|
||||
App::new().service(
|
||||
actix_files::Files::new("/", dir.as_ref())
|
||||
actix_files::Files::new("/", dir.clone())
|
||||
.show_files_listing()
|
||||
.prefer_utf8(true),
|
||||
)
|
||||
})
|
||||
.workers(available_parallelism().map_or(1, NonZeroUsize::get))
|
||||
.workers(available_parallelism().unwrap().get())
|
||||
.bind(sock)?
|
||||
.run()
|
||||
.await
|
||||
|
Loading…
x
Reference in New Issue
Block a user