diff options
| author | rtkay123 <dev@kanjala.com> | 2025-11-23 17:21:14 +0200 |
|---|---|---|
| committer | rtkay123 <dev@kanjala.com> | 2025-11-23 17:21:14 +0200 |
| commit | 6c7e7466cdca167c1f28030886b7d01ba346d2c9 (patch) | |
| tree | 6b77751b5d556e12be3e1645a3b47896c4186cdd | |
| parent | ae72e4f8d4ccb6d5ed71e17d6b2ffb0ac8876e0a (diff) | |
| download | sellershut-master.tar.bz2 sellershut-master.zip | |
| -rw-r--r-- | Cargo.lock | 11 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | crates/sellershut/Cargo.toml | 2 | ||||
| -rw-r--r-- | crates/sellershut/src/config.rs | 26 | ||||
| -rw-r--r-- | crates/sellershut/src/config/port.rs | 18 | ||||
| -rw-r--r-- | crates/sellershut/src/main.rs | 1 |
6 files changed, 57 insertions, 3 deletions
@@ -1787,6 +1787,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] +name = "secrecy" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e891af845473308773346dc847b2c23ee78fe442e0472ac50e22a18a93d3ae5a" +dependencies = [ + "zeroize", +] + +[[package]] name = "sellershut" version = "0.1.0" dependencies = [ @@ -1794,11 +1803,13 @@ dependencies = [ "anyhow", "axum", "clap", + "secrecy", "tokio", "tower", "tower-http", "tracing", "tracing-subscriber", + "url", "utoipa", "utoipa-axum", "utoipa-rapidoc", @@ -10,5 +10,7 @@ description = "A federated marketplace platform" [workspace.dependencies] activitypub_federation = { version = "0.7.0-beta.8", default-features = false } +secrecy = "0.10.3" serde = "1.0.228" tracing = "0.1.41" +url = "2.5.7" diff --git a/crates/sellershut/Cargo.toml b/crates/sellershut/Cargo.toml index aee606e..e236b6b 100644 --- a/crates/sellershut/Cargo.toml +++ b/crates/sellershut/Cargo.toml @@ -12,11 +12,13 @@ activitypub_federation = { workspace = true, features = ["axum"] } anyhow = "1.0.100" axum = { version = "0.8.7", features = ["macros"] } clap = { version = "4.5.53", features = ["derive", "env"] } +secrecy.workspace = true tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread", "signal"] } tower = "0.5.2" tower-http = { version = "0.6.6", features = ["propagate-header", "request-id", "trace", "timeout"] } tracing.workspace = true tracing-subscriber = { version = "0.3.20", features = ["env-filter"] } +url.workspace = true utoipa = "5.4.0" utoipa-axum = "0.2.0" utoipa-rapidoc = { version = "6.0.0", features = ["axum"], optional = true } diff --git a/crates/sellershut/src/config.rs b/crates/sellershut/src/config.rs index 65383a6..5a63bcc 100644 --- a/crates/sellershut/src/config.rs +++ b/crates/sellershut/src/config.rs @@ -1,17 +1,22 @@ mod logging; +mod port; -use std::path::PathBuf; +use std::{path::PathBuf, str::FromStr}; use clap::Parser; use logging::LogLevel; +use url::Url; + +use crate::config::port::port_in_range; pub const LOG_LEVEL: &str = "LOG_LEVEL"; +const DEFAULT_DB: &str = "postgres://postgres:password@localhost:5432/postgres"; #[derive(Parser, Debug)] -#[command(version, about, long_about = None)] +#[command(version, about, long_about = None, name = env!("CARGO_PKG_NAME"))] pub struct Cli { /// Sets the port the server listens on - #[arg(short, long, default_value_t = 2210, env = "PORT")] + #[arg(short, long, default_value_t = 2210, env = "PORT", value_parser = port_in_range)] pub port: u16, /// Sets the application log level @@ -25,6 +30,10 @@ pub struct Cli { /// Request timeout duration (in seconds) #[arg(short, long, default_value_t = 10, env = "TIMEOUT_DURATION")] pub timeout_duration: u64, + + /// Database URL + #[arg(short, long, value_name = "DB_URL", env = "DB_URL", default_value_t = Url::from_str(DEFAULT_DB).expect("database_url"))] + pub database_url: Url, } impl Cli { @@ -32,3 +41,14 @@ impl Cli { self.log_level.into() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn verify_cli() { + use clap::CommandFactory; + Cli::command().debug_assert(); + } +} diff --git a/crates/sellershut/src/config/port.rs b/crates/sellershut/src/config/port.rs new file mode 100644 index 0000000..c7c8d62 --- /dev/null +++ b/crates/sellershut/src/config/port.rs @@ -0,0 +1,18 @@ +use std::ops::RangeInclusive; + +const PORT_RANGE: RangeInclusive<usize> = 1..=65535; + +pub fn port_in_range(s: &str) -> Result<u16, String> { + let port: usize = s + .parse() + .map_err(|_| format!("`{s}` isn't a port number"))?; + if PORT_RANGE.contains(&port) { + Ok(port as u16) + } else { + Err(format!( + "port not in range {}-{}", + PORT_RANGE.start(), + PORT_RANGE.end() + )) + } +} diff --git a/crates/sellershut/src/main.rs b/crates/sellershut/src/main.rs index 07e6193..c6ac12f 100644 --- a/crates/sellershut/src/main.rs +++ b/crates/sellershut/src/main.rs @@ -13,6 +13,7 @@ use crate::logging::initialise_logging; #[tokio::main] async fn main() -> anyhow::Result<()> { let config = config::Cli::parse(); + dbg!(&config); initialise_logging(&config); let app = server::router(&config); |
