1
Fork 0
mirror of https://github.com/RGBCube/Site synced 2025-08-01 13:37:49 +00:00

Add basic CLI logic and make log level specifiable

This commit is contained in:
RGBCube 2023-12-30 14:29:53 +03:00
parent ffbf1085af
commit 1010f21fe0
No known key found for this signature in database
5 changed files with 181 additions and 32 deletions

151
Cargo.lock generated
View file

@ -242,6 +242,60 @@ dependencies = [
"alloc-no-stdlib", "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]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -358,6 +412,52 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 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]] [[package]]
name = "convert_case" name = "convert_case"
version = "0.4.0" version = "0.4.0"
@ -405,9 +505,9 @@ dependencies = [
[[package]] [[package]]
name = "deranged" name = "deranged"
version = "0.3.10" version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eb30d70a07a3b04884d2677f06bec33509dc67ca60d92949e5535352d3191dc" checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
dependencies = [ dependencies = [
"powerfmt", "powerfmt",
] ]
@ -610,6 +710,12 @@ version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.3" version = "0.3.3"
@ -766,6 +872,9 @@ name = "log"
version = "0.4.20" version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "maud" name = "maud"
@ -845,16 +954,6 @@ dependencies = [
"windows-sys 0.48.0", "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]] [[package]]
name = "object" name = "object"
version = "0.32.2" version = "0.32.2"
@ -1181,8 +1280,10 @@ name = "site"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"anyhow",
"bytes", "bytes",
"cargo_toml", "cargo_toml",
"clap",
"embed", "embed",
"env_logger", "env_logger",
"log", "log",
@ -1190,7 +1291,6 @@ dependencies = [
"mime_guess", "mime_guess",
"minify-js", "minify-js",
"tar", "tar",
"tokio",
] ]
[[package]] [[package]]
@ -1218,6 +1318,12 @@ dependencies = [
"windows-sys 0.48.0", "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]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"
@ -1314,26 +1420,13 @@ dependencies = [
"bytes", "bytes",
"libc", "libc",
"mio", "mio",
"num_cpus",
"parking_lot", "parking_lot",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry", "signal-hook-registry",
"socket2", "socket2",
"tokio-macros",
"windows-sys 0.48.0", "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]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.7.10" version = "0.7.10"
@ -1449,6 +1542,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View file

@ -10,16 +10,17 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4.4.1" actix-web = "4.4.1"
anyhow = "1.0.77"
bytes = "1.5.0" bytes = "1.5.0"
cargo_toml = "0.17.2" cargo_toml = "0.17.2"
clap = { version = "4.4.12", features = [ "derive" ] }
embed = { git = "https://github.com/RGBCube/embed-rs" } embed = { git = "https://github.com/RGBCube/embed-rs" }
env_logger = "0.10.1" env_logger = "0.10.1"
log = "0.4.20" log = { version = "0.4.20", features = [ "serde" ] }
maud = { version = "0.25.0", features = ["actix-web"] } maud = { version = "0.25.0", features = [ "actix-web" ] }
mime_guess = "2.0.4" mime_guess = "2.0.4"
minify-js = "0.6.0" minify-js = "0.6.0"
tar = "0.4.40" tar = "0.4.40"
tokio = { version = "1.35.1", features = ["full"] }
[build] [build]
incremental = true incremental = true

View file

@ -24,6 +24,12 @@ Specifies on which port the site service listens for connections.
Default: 8080. Default: 8080.
#### `services.site.logLevel`
The log level to log stuff with.
Default: info.
#### `services.site.openFirewall` #### `services.site.openFirewall`
Whether to open the firewall port for the tcp listener. Whether to open the firewall port for the tcp listener.

View file

@ -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 { openFirewall = mkOption {
type = types.bool; type = types.bool;
default = false; default = false;
@ -81,7 +90,7 @@
capabilities = [ "" ] ++ optionals needsPrivileges [ "CAP_NET_BIND_SERVICE" ]; capabilities = [ "" ] ++ optionals needsPrivileges [ "CAP_NET_BIND_SERVICE" ];
rootDirectory = "/run/site"; rootDirectory = "/run/site";
in { 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"; Restart = "always";
DynamicUser = true; DynamicUser = true;
RootDirectory = rootDirectory; RootDirectory = rootDirectory;

View file

@ -1,3 +1,37 @@
fn main() { use actix_web::{
println!("Hello, world!"); 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(())
} }