From 981e1466969dbd352c4ea3d81139fa9c257d8d61 Mon Sep 17 00:00:00 2001 From: asonix Date: Sat, 6 Jun 2020 19:54:06 -0500 Subject: [PATCH] Add StructOpt --- Cargo.lock | 191 +++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 + src/config.rs | 24 +++++++ src/main.rs | 10 ++- 4 files changed, 222 insertions(+), 5 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 5170089..d203e19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,7 +74,7 @@ dependencies = [ "actix-service", "actix-threadpool", "actix-utils", - "base64", + "base64 0.12.1", "bitflags", "brotli2", "bytes", @@ -330,6 +330,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi 0.3.8", +] + [[package]] name = "anyhow" version = "1.0.31" @@ -380,7 +389,7 @@ dependencies = [ "actix-http", "actix-rt", "actix-service", - "base64", + "base64 0.12.1", "bytes", "derive_more", "futures-core", @@ -412,6 +421,12 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b20b618342cf9891c292c4f5ac2cde7287cc5c87e87e9c769d617793607dec1" +[[package]] +name = "base64" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" + [[package]] name = "base64" version = "0.12.1" @@ -516,6 +531,21 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "clap" +version = "2.33.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -968,6 +998,19 @@ dependencies = [ "tiff", ] +[[package]] +name = "img_hash" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df6c5bc88f37a165c63143e38924f691246fc77f12de0cbd126fe0c8ca3527b" +dependencies = [ + "base64 0.11.0", + "image", + "rustdct", + "serde", + "transpose 0.2.0", +] + [[package]] name = "indexmap" version = "1.4.0" @@ -1191,6 +1234,16 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "num-complex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95" +dependencies = [ + "autocfg", + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.42" @@ -1303,12 +1356,14 @@ dependencies = [ "env_logger", "futures", "image", + "img_hash", "log", "mime", "rand", "serde_json", "sha2", "sled", + "structopt", "thiserror", ] @@ -1362,6 +1417,32 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" +[[package]] +name = "proc-macro-error" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98e9e4b82e0ef281812565ea4751049f1bdcdfccda7d3f459f2e138a40c08678" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f5444ead4e9935abd7f27dc51f7e852a0569ac888096d5ec2499470794e2e53" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "syn-mid", + "version_check", +] + [[package]] name = "proc-macro-hack" version = "0.5.16" @@ -1512,6 +1593,28 @@ dependencies = [ "semver", ] +[[package]] +name = "rustdct" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef4d167674b4cf68c2114bdbcd34c95aa9071652b73b0f43b19298f1d2780b7d" +dependencies = [ + "rustfft", +] + +[[package]] +name = "rustfft" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77008ed59a8923c8b4ac2e5eaa6d28fbe893d3b9515098a4a5fc7767d6430fe5" +dependencies = [ + "num-complex", + "num-integer", + "num-traits", + "strength_reduce", + "transpose 0.1.0", +] + [[package]] name = "ryu" version = "1.0.5" @@ -1727,6 +1830,42 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" +[[package]] +name = "strength_reduce" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3ff2f71c82567c565ba4b3009a9350a96a7269eaa4001ebedae926230bc2254" + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "structopt" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863246aaf5ddd0d6928dfeb1a9ca65f505599e4e1b399935ef7e75107516b4ef" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d239ca4b13aee7a2142e6795cbd69e457665ff8037aed33b3effdc430d2f927a" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "syn" version = "1.0.30" @@ -1738,6 +1877,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "syn-mid" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "termcolor" version = "1.1.0" @@ -1747,6 +1897,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.19" @@ -1889,6 +2048,22 @@ dependencies = [ "tokio", ] +[[package]] +name = "transpose" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "643e21580bb0627c7bb09e5cedbb42c8705b19d012de593ed6b0309270b3cd1e" + +[[package]] +name = "transpose" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3311ef71dea6a1fd6bf5bfc10ec5b4bef6174048f6b481dbc6ce915ff48c0a0" +dependencies = [ + "num-integer", + "strength_reduce", +] + [[package]] name = "trust-dns-proto" version = "0.19.5" @@ -1975,6 +2150,12 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" + [[package]] name = "unicode-xid" version = "0.2.0" @@ -1992,6 +2173,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.2" diff --git a/Cargo.toml b/Cargo.toml index 9823b7f..dc18ba6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,10 +16,12 @@ bytes = "0.5" env_logger = "0.7" futures = "0.3.4" image = "0.23.4" +img_hash = "3.1.0" log = "0.4" mime = "0.3.1" rand = "0.7.3" serde_json = "1.0" sha2 = "0.8.2" sled = "0.32.0-rc1" +structopt = "0.3.14" thiserror = "1.0" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..420f9e5 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,24 @@ +use std::{net::SocketAddr, path::PathBuf}; + +#[derive(structopt::StructOpt)] +pub struct Config { + #[structopt( + short, + long, + help = "The address and port the server binds to, e.g. 127.0.0.1:80" + )] + addr: SocketAddr, + + #[structopt(short, long, help = "The path to the data directory, e.g. data/")] + path: PathBuf, +} + +impl Config { + pub(crate) fn bind_address(&self) -> SocketAddr { + self.addr + } + + pub(crate) fn data_dir(&self) -> PathBuf { + self.path.clone() + } +} diff --git a/src/main.rs b/src/main.rs index 0dbf650..038107f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,11 +8,13 @@ use actix_web::{ use futures::stream::{Stream, TryStreamExt}; use log::{error, info}; use std::path::PathBuf; +use structopt::StructOpt; +mod config; mod error; mod upload_manager; -use self::{error::UploadError, upload_manager::UploadManager}; +use self::{config::Config, error::UploadError, upload_manager::UploadManager}; const ACCEPTED_MIMES: &[mime::Mime] = &[ mime::IMAGE_BMP, @@ -234,8 +236,10 @@ where #[actix_rt::main] async fn main() -> Result<(), anyhow::Error> { + let config = Config::from_args(); + std::env::set_var("RUST_LOG", "info"); env_logger::init(); - let manager = UploadManager::new("data/".to_string().into()).await?; + let manager = UploadManager::new(config.data_dir()).await?; // Create a new Multipart Form validator // @@ -275,7 +279,7 @@ async fn main() -> Result<(), anyhow::Error> { ), ) }) - .bind("127.0.0.1:8080")? + .bind(config.bind_address())? .run() .await?;