diff --git a/Cargo.toml b/Cargo.toml index 78c9155..98b2369 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ readme = "README.md" [dependencies] actix-web = "0.7" bytes = "0.4" +clap = "2" env_logger = "*" futures = "0.1" tar = "0.4" diff --git a/README.md b/README.md index 92a67ab..09012ee 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,22 @@ a simple http server like `python -m http.server` but: * maybe announce itself on mDNS (avahi) * maybe compress -Usage [TODO]: - ``` -http-server [--bind ADDRESS] [--chdir DIRECTORY] [port] +USAGE: + http-server [OPTIONS] [port] - port Specify alternate port [default: 8000] - --bind ADDRESS Specify alternate bind address [default: all interfaces] - --chdir DIRECTORY Specify directory to server [default: current directory] +FLAGS: + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + --bind
Specify alternate bind address [default: 0.0.0.0] + --chdir Specify directory to server [default: .] + +ARGS: + Specify alternate port [default: 8000] ``` ## FAQ -* Q: why .tar and not .zip? A: you can't stream a zip file efficiently, it needs to write back in a file. +* Q: why .tar and not .zip? A: you can't stream a zip file efficiently, it needs to seek and write to the beggining of a file. diff --git a/src/main.rs b/src/main.rs index 421b847..8cb5045 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,25 +5,53 @@ extern crate futures; extern crate tar; extern crate htmlescape; extern crate percent_encoding; +#[macro_use] +extern crate clap; mod channel; mod web; use actix_web::server; use actix_web::actix; +use clap::Arg; -use std::env; use std::io; -// TODO cli args fn main() -> Result<(), io::Error> { + let app = clap::App::new(crate_name!()) + .author(crate_authors!("\n")) + .version(crate_version!()) + .about(crate_description!()) + .arg(Arg::with_name("chdir") + .long("chdir") + .value_name("DIRECTORY") + .help("Specify directory to server") + .default_value(".") + .takes_value(true)) + .arg(Arg::with_name("addr") + .long("bind") + .value_name("ADDRESS") + .help("Specify alternate bind address") + .default_value("0.0.0.0") + .takes_value(true)) + .arg(Arg::with_name("port") + .help("Specify alternate port") + .default_value("8000") + .index(1)); + let matches = app.get_matches(); + + let chdir = matches.value_of("chdir").unwrap(); + let port = matches.value_of("port").unwrap(); + let addr = matches.value_of("addr").unwrap(); + let bind_addr = format!("{}:{}", addr, port); + std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); - let bind_addr = env::var("HTTP_ADDR").unwrap_or(String::from("0.0.0.0:8000")); let sys = actix::System::new("http_server_rs"); - server::new(web::create_app) + let chdir = String::from(chdir); + server::new(move || web::create_app(&chdir)) .bind(&bind_addr) .expect(&format!("Can't listen on {} ", bind_addr)) .start(); diff --git a/src/web.rs b/src/web.rs index ed2d0e1..df65fc3 100644 --- a/src/web.rs +++ b/src/web.rs @@ -8,8 +8,8 @@ use channel; use std::path::PathBuf; use std; -pub fn create_app() -> App { - let static_files = fs::StaticFiles::new(".").unwrap().show_files_listing().files_listing_renderer(handle_directory); +pub fn create_app(directory: &str) -> App { + let static_files = fs::StaticFiles::new(directory).unwrap().show_files_listing().files_listing_renderer(handle_directory); App::new() .middleware(middleware::Logger::new(r#"%a "%r" %s %b "%{Referer}i" "%{User-Agent}i" %T"#)) .resource(r"/{tail:.*}.tar", |r| r.method(Method::GET).f(handle_tar))