implement cli arguments with clap
* add clap dependency https://docs.rs/clap/ * updated README with the real USAGE * the web App now takes a directory to server files from
This commit is contained in:
parent
933b53b2b1
commit
e400d6e1d3
4 changed files with 48 additions and 13 deletions
|
@ -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"
|
||||
|
|
20
README.md
20
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 <ADDRESS> Specify alternate bind address [default: 0.0.0.0]
|
||||
--chdir <DIRECTORY> Specify directory to server [default: .]
|
||||
|
||||
ARGS:
|
||||
<port> 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.
|
||||
|
|
36
src/main.rs
36
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();
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Reference in a new issue