From 6f710add0f291ac97697093beefd3c2f229e97da Mon Sep 17 00:00:00 2001 From: volker Date: Wed, 14 Feb 2024 16:22:15 +0100 Subject: [PATCH] Added cmd line parsing --- Cargo.lock | 141 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/main.rs | 26 +++++++- src/webserver_main.rs | 48 -------------- 4 files changed, 166 insertions(+), 52 deletions(-) delete mode 100644 src/webserver_main.rs diff --git a/Cargo.lock b/Cargo.lock index e8700fc..70dc903 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,12 +11,32 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "anyhow" version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -74,6 +94,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cpufeatures" version = "0.2.7" @@ -274,6 +309,24 @@ dependencies = [ "http", ] +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.2.6" @@ -396,6 +449,7 @@ dependencies = [ "rust-embed", "serde", "serde_json", + "structopt", "thiserror", "tokio", "warp", @@ -420,6 +474,12 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.144" @@ -586,6 +646,30 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.56" @@ -849,6 +933,36 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[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.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "syn" version = "1.0.109" @@ -880,6 +994,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.40" @@ -1070,6 +1193,18 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" + +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + [[package]] name = "url" version = "2.3.1" @@ -1087,6 +1222,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[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.4" diff --git a/Cargo.toml b/Cargo.toml index 82822d7..728d5c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ warp = "0.3" warp-embed = "0.4" rust-embed = "6" log = "0.4" -env_logger = "0.10" \ No newline at end of file +env_logger = "0.10" +structopt = "0.3.21" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 92f4e82..bc6aa97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,11 +9,23 @@ use warp::http::{StatusCode, Uri}; use rust_embed::RustEmbed; use warp::reply::json; use serde_json::json; +use std::net::IpAddr; +use structopt::StructOpt; #[derive(RustEmbed)] #[folder = "web/dist"] struct Static; +#[derive(Debug, StructOpt)] +struct Opt { + #[structopt(long, default_value = "localhost")] + host: String, + + #[structopt(long, default_value = "3030")] + port: u16, +} + + #[tokio::main] async fn main() { @@ -62,8 +74,16 @@ async fn main() { .allow_methods(vec!["GET", "POST"]) .allow_headers(vec!["Content-Type"]); - log::info!("Starting server on http://localhost:3030"); + + let opt = Opt::from_args(); + + log::info!("Starting server on http://{}:{}", opt.host, opt.port); warp::serve(iptables.or(redirect).or(static_files).or(error).with(cors)) - .run(([127, 0, 0, 1], 3030)) + .run((opt.host.parse::().unwrap(), opt.port)) .await; - } + +} + + + + \ No newline at end of file diff --git a/src/webserver_main.rs b/src/webserver_main.rs deleted file mode 100644 index 46eb299..0000000 --- a/src/webserver_main.rs +++ /dev/null @@ -1,48 +0,0 @@ -mod errors; -mod iptables; - -use crate::iptables::{iptables_save, IptablesState}; - -use warp::Filter; - -use rust_embed::RustEmbed; - -#[derive(RustEmbed)] -#[folder = "web/dist"] -struct Static; - -#[tokio::main] -async fn main() { - - if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "info"); - } - env_logger::init(); - - // GET /hello/warp => 200 OK with body "Hello, warp!" - let hello = warp::path!("myapp" / "hello" / String) - .map(|name| format!("Hello, {}!", name)); - - let static_files = warp::path("myapp") - .and(warp::get()) - .and(warp_embed::embed(&Static)) - .boxed(); - - let iptables = warp::path!("iptables") - .then(|| async { - let out = iptables_save().await.unwrap(); - let mut state = IptablesState::new(); - state.parse(out).await.unwrap(); - format!("{:#?}", state) - }); - - let cors = warp::cors() - .allow_any_origin() - .allow_methods(vec!["GET", "POST"]) - .allow_headers(vec!["Content-Type"]); - - log::info!("Starting server on http://localhost:3030"); - warp::serve(hello.or(static_files).with(cors)) - .run(([127, 0, 0, 1], 3030)) - .await; -} \ No newline at end of file