From 1010f21fe0138a6aec553fd84d03a122df0ac5ee Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 30 Dec 2023 14:29:53 +0300 Subject: [PATCH] Add basic CLI logic and make log level specifiable --- Cargo.lock | 151 +++++++++++++++++++++++++++++++++++++++++++--------- Cargo.toml | 7 +-- README.md | 6 +++ flake.nix | 11 +++- src/main.rs | 38 ++++++++++++- 5 files changed, 181 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0abdb88..f6c73e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -242,6 +242,60 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "anstream" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + +[[package]] +name = "anyhow" +version = "1.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9d19de80eff169429ac1e9f48fffb163916b448a44e8e046186232046d9e1f9" + [[package]] name = "autocfg" version = "1.1.0" @@ -358,6 +412,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcfab8ba68f3668e89f6ff60f5b205cea56aa7b769451a59f34b8682f51c056d" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb7fb5e4e979aec3be7791562fcba452f94ad85e954da024396433e0e25a79e9" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.43", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "convert_case" version = "0.4.0" @@ -405,9 +505,9 @@ dependencies = [ [[package]] name = "deranged" -version = "0.3.10" +version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8eb30d70a07a3b04884d2677f06bec33509dc67ca60d92949e5535352d3191dc" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" dependencies = [ "powerfmt", ] @@ -610,6 +710,12 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.3" @@ -766,6 +872,9 @@ name = "log" version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +dependencies = [ + "serde", +] [[package]] name = "maud" @@ -845,16 +954,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "num_cpus" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" -dependencies = [ - "hermit-abi", - "libc", -] - [[package]] name = "object" version = "0.32.2" @@ -1181,8 +1280,10 @@ name = "site" version = "0.0.1" dependencies = [ "actix-web", + "anyhow", "bytes", "cargo_toml", + "clap", "embed", "env_logger", "log", @@ -1190,7 +1291,6 @@ dependencies = [ "mime_guess", "minify-js", "tar", - "tokio", ] [[package]] @@ -1218,6 +1318,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.109" @@ -1314,26 +1420,13 @@ dependencies = [ "bytes", "libc", "mio", - "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", - "tokio-macros", "windows-sys 0.48.0", ] -[[package]] -name = "tokio-macros" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.43", -] - [[package]] name = "tokio-util" version = "0.7.10" @@ -1449,6 +1542,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 1244bd7..47d669f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,16 +10,17 @@ edition = "2021" [dependencies] actix-web = "4.4.1" +anyhow = "1.0.77" bytes = "1.5.0" cargo_toml = "0.17.2" +clap = { version = "4.4.12", features = [ "derive" ] } embed = { git = "https://github.com/RGBCube/embed-rs" } env_logger = "0.10.1" -log = "0.4.20" -maud = { version = "0.25.0", features = ["actix-web"] } +log = { version = "0.4.20", features = [ "serde" ] } +maud = { version = "0.25.0", features = [ "actix-web" ] } mime_guess = "2.0.4" minify-js = "0.6.0" tar = "0.4.40" -tokio = { version = "1.35.1", features = ["full"] } [build] incremental = true diff --git a/README.md b/README.md index 9d1f4a2..abbf6c2 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,12 @@ Specifies on which port the site service listens for connections. Default: 8080. +#### `services.site.logLevel` + +The log level to log stuff with. + +Default: info. + #### `services.site.openFirewall` Whether to open the firewall port for the tcp listener. diff --git a/flake.nix b/flake.nix index 136e38d..0329267 100644 --- a/flake.nix +++ b/flake.nix @@ -60,6 +60,15 @@ ''; }; + logLevel = mkOption { + type = types.enum [ "off" "error" "warn" "info" "debug" "trace" ]; + default = "info"; + example = "warn"; + description = mdDoc '' + Specifies the log level that the site service will log stuff with. + ''; + }; + openFirewall = mkOption { type = types.bool; default = false; @@ -81,7 +90,7 @@ capabilities = [ "" ] ++ optionals needsPrivileges [ "CAP_NET_BIND_SERVICE" ]; rootDirectory = "/run/site"; in { - ExecStart = "${self.packages.${pkgs.system}.site}/bin/site --port ${cfg.port}"; + ExecStart = "${self.packages.${pkgs.system}.site}/bin/site --port ${cfg.port} --log-level ${cfg.logLevel}"; Restart = "always"; DynamicUser = true; RootDirectory = rootDirectory; diff --git a/src/main.rs b/src/main.rs index e7a11a9..b34f974 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,37 @@ -fn main() { - println!("Hello, world!"); +use actix_web::{ + main as async_main, + App, + HttpServer, +}; +use anyhow::Context; +use clap::Parser; + +#[derive(Parser)] +#[command(author, version, about)] +struct Cli { + /// The port to listen for connections on. + #[arg(long, default_value = "8080")] + port: u16, + /// The log level to log stuff with. + #[arg(long, default_value = "info")] + log_level: log::LevelFilter, +} + +#[async_main] +async fn main() -> anyhow::Result<()> { + let args = Cli::parse(); + + env_logger::builder() + .filter_level(args.log_level) + .target(env_logger::Target::Stdout) + .init(); + + HttpServer::new(|| App::new()) + .bind(("0.0.0.0", args.port)) + .with_context(|| format!("Failed to bind to 0.0.0.0:{port}", port = args.port))? + .run() + .await + .with_context(|| "Failed to run HttpServer")?; + + Ok(()) }