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]
|
[dependencies]
|
||||||
actix-web = "0.7"
|
actix-web = "0.7"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
|
clap = "2"
|
||||||
env_logger = "*"
|
env_logger = "*"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
tar = "0.4"
|
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 announce itself on mDNS (avahi)
|
||||||
* maybe compress
|
* maybe compress
|
||||||
|
|
||||||
Usage [TODO]:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
http-server [--bind ADDRESS] [--chdir DIRECTORY] [port]
|
USAGE:
|
||||||
|
http-server [OPTIONS] [port]
|
||||||
|
|
||||||
port Specify alternate port [default: 8000]
|
FLAGS:
|
||||||
--bind ADDRESS Specify alternate bind address [default: all interfaces]
|
-h, --help Prints help information
|
||||||
--chdir DIRECTORY Specify directory to server [default: current directory]
|
-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
|
## 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 tar;
|
||||||
extern crate htmlescape;
|
extern crate htmlescape;
|
||||||
extern crate percent_encoding;
|
extern crate percent_encoding;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate clap;
|
||||||
|
|
||||||
mod channel;
|
mod channel;
|
||||||
mod web;
|
mod web;
|
||||||
|
|
||||||
use actix_web::server;
|
use actix_web::server;
|
||||||
use actix_web::actix;
|
use actix_web::actix;
|
||||||
|
use clap::Arg;
|
||||||
|
|
||||||
use std::env;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
// TODO cli args
|
|
||||||
fn main() -> Result<(), io::Error> {
|
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");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
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");
|
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)
|
.bind(&bind_addr)
|
||||||
.expect(&format!("Can't listen on {} ", bind_addr))
|
.expect(&format!("Can't listen on {} ", bind_addr))
|
||||||
.start();
|
.start();
|
||||||
|
|
|
@ -8,8 +8,8 @@ use channel;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std;
|
use std;
|
||||||
|
|
||||||
pub fn create_app() -> App {
|
pub fn create_app(directory: &str) -> App {
|
||||||
let static_files = fs::StaticFiles::new(".").unwrap().show_files_listing().files_listing_renderer(handle_directory);
|
let static_files = fs::StaticFiles::new(directory).unwrap().show_files_listing().files_listing_renderer(handle_directory);
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Logger::new(r#"%a "%r" %s %b "%{Referer}i" "%{User-Agent}i" %T"#))
|
.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))
|
.resource(r"/{tail:.*}.tar", |r| r.method(Method::GET).f(handle_tar))
|
||||||
|
|
Loading…
Add table
Reference in a new issue