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]
|
[package]
|
||||||
name = "expose-dir-via-http"
|
name = "expose-dir-via-http"
|
||||||
version = "1.3.20"
|
version = "1.0.0"
|
||||||
edition = "2024"
|
edition = "2021"
|
||||||
|
|
||||||
[lints.rust]
|
|
||||||
unsafe_code = "forbid"
|
|
||||||
|
|
||||||
[lints.clippy]
|
|
||||||
pedantic = { level = "warn", priority = -1 }
|
|
||||||
todo = "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"] }
|
||||||
|
|
||||||
|
31
src/main.rs
31
src/main.rs
@ -1,20 +1,18 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
env,
|
||||||
net::{IpAddr, SocketAddr},
|
net::{IpAddr, SocketAddr},
|
||||||
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,36 +23,23 @@ 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),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.workers(available_parallelism().map_or(1, NonZeroUsize::get))
|
.workers(available_parallelism().unwrap().get())
|
||||||
.bind(sock)?
|
.bind(sock)?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
|
Loading…
x
Reference in New Issue
Block a user