diff options
| author | rtkay123 <dev@kanjala.com> | 2026-02-03 13:45:46 +0200 |
|---|---|---|
| committer | rtkay123 <dev@kanjala.com> | 2026-02-03 13:45:46 +0200 |
| commit | eb2e86997d47249aa31b703598de13ab2eb96caa (patch) | |
| tree | 9a591adee7d027b305d07a04987b5559b99f4d37 /src/config/cli | |
| parent | 0ea3cb1d4743b922fbc6e07037096e75caffba8f (diff) | |
| download | sellershut-eb2e86997d47249aa31b703598de13ab2eb96caa.tar.bz2 sellershut-eb2e86997d47249aa31b703598de13ab2eb96caa.zip | |
Diffstat (limited to 'src/config/cli')
| -rw-r--r-- | src/config/cli/cache/mod.rs | 46 | ||||
| -rw-r--r-- | src/config/cli/mod.rs | 89 | ||||
| -rw-r--r-- | src/config/cli/oauth/mod.rs | 36 |
3 files changed, 171 insertions, 0 deletions
diff --git a/src/config/cli/cache/mod.rs b/src/config/cli/cache/mod.rs new file mode 100644 index 0000000..04b36bc --- /dev/null +++ b/src/config/cli/cache/mod.rs @@ -0,0 +1,46 @@ +use clap::{Parser, ValueEnum}; +use serde::Deserialize; +use url::Url; + +#[derive(Debug, Clone, Parser, Deserialize, Default)] +pub struct Cache { + /// Cache connection string + #[arg(long, env = "CACHE_URL", default_value = "redis://localhost:6379")] + pub cache_url: Option<Url>, + #[arg(long, env = "CACHE_POOL_ENABLED", default_value = "true")] + pub cache_pooled: Option<bool>, + #[serde(rename = "type")] + #[arg(long, env = "CACHE_TYPE", default_value = "non-clustered")] + pub cache_type: Option<RedisVariant>, + #[serde(default = "default_max_conns")] + #[serde(rename = "max-connections")] + #[arg(long, env = "CACHE_MAX_CONNECTIONS", default_value = "100")] + pub cache_max_conn: Option<u16>, + #[command(flatten)] + pub sentinel_config: SentinelConfig, +} + +#[derive(Debug, Deserialize, Clone, ValueEnum)] +#[serde(rename_all = "kebab-case")] +pub enum RedisVariant { + Clustered, + NonClustered, + Sentinel, +} + +fn default_max_conns() -> Option<u16> { + Some(100) +} + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Parser, Default)] +pub struct SentinelConfig { + #[serde(rename = "sentinel_service_name")] + #[arg(long, env = "CACHE_SENTINEL_NAME", default_value = "true")] + pub service_name: Option<String>, + #[serde(default)] + #[arg(long, env = "CACHE_TLS_MODE_SECURE")] + pub cache_tls_mode_secure: bool, + #[serde(default)] + #[arg(long, env = "CACHE_USE_RESP3")] + pub cache_use_resp3: bool, +} diff --git a/src/config/cli/mod.rs b/src/config/cli/mod.rs new file mode 100644 index 0000000..81eb2fe --- /dev/null +++ b/src/config/cli/mod.rs @@ -0,0 +1,89 @@ +pub mod cache; + +#[cfg(feature = "oauth")] +pub mod oauth; + +use std::path::PathBuf; + +use clap::Parser; +use url::Url; + +use crate::config::{logging::LogLevel, port::port_in_range}; + +#[derive(Parser, Debug)] +#[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 = "2210", env = "PORT", value_parser = port_in_range)] + pub port: Option<u16>, + + /// Server's domain + #[arg(short, long, default_value = "localhost", env = "DOMAIN")] + pub domain: Option<String>, + + /// Sets a custom config file + #[arg(short, long, value_name = "FILE")] + pub config: Option<PathBuf>, + + /// Sets the application log level + #[arg(short, long, value_enum, env = "LOG_LEVEL", default_value = "debug")] + pub log_level: Option<LogLevel>, + + /// Request timeout duration (in seconds) + #[arg(short, long, env = "TIMEOUT_SECONDS", default_value = "10")] + pub timeout_duration: Option<u64>, + + /// Database connection string + #[arg( + long, + env = "DATABASE_URL", + default_value = "postgres://postgres:password@localhost:5432/sellershut" + )] + pub db: Option<Url>, + + #[command(flatten)] + pub cache: Option<cache::Cache>, + + /// Server's system name + #[arg(short, long, default_value = "sellershut", env = "SYSTEM_NAME")] + pub system_name: Option<String>, + + /// Server's system name + #[arg(short, long, default_value = "prod", env = "SYSTEM_NAME")] + pub environment: Option<String>, + + /// Oauth optionas + #[command(flatten)] + #[cfg(feature = "oauth")] + pub oauth: oauth::OAuth, +} + +#[cfg(test)] +impl Default for Cli { + fn default() -> Self { + let url = Url::parse("postgres://postgres:password@localhost:5432/sellershut").ok(); + Self { + port: Default::default(), + config: Default::default(), + log_level: Default::default(), + timeout_duration: Some(10), + domain: Default::default(), + system_name: Default::default(), + environment: Default::default(), + oauth: Default::default(), + cache: Default::default(), + db: url, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn verify_cli() { + use clap::CommandFactory; + Cli::command().debug_assert(); + } +} diff --git a/src/config/cli/oauth/mod.rs b/src/config/cli/oauth/mod.rs new file mode 100644 index 0000000..4bf1c34 --- /dev/null +++ b/src/config/cli/oauth/mod.rs @@ -0,0 +1,36 @@ +use clap::Parser; +#[cfg(feature = "oauth-discord")] +use secrecy::SecretString; +use serde::Deserialize; +#[cfg(feature = "oauth")] +use url::Url; + +#[derive(Debug, Clone, Parser, Deserialize, Default)] +pub struct OAuth { + #[cfg(feature = "oauth-discord")] + #[command(flatten)] + discord: DiscordOauth, + #[arg(long, env = "OAUTH_REDIRECT_URL")] + oauth_redirect_url: Option<Url>, +} + +#[cfg(feature = "oauth-discord")] +#[derive(Debug, Clone, Parser, Deserialize, Default)] +pub struct DiscordOauth { + #[arg(long, env = "OAUTH_DISCORD_CLIENT_ID")] + discord_client_id: Option<String>, + #[arg(long, env = "OAUTH_DISCORD_CLIENT_SECRET")] + discord_client_secret: Option<SecretString>, + #[arg( + long, + env = "OAUTH_DISCORD_TOKEN_URL", + default_value = "https://discord.com/api/oauth2/token" + )] + discord_token_url: Option<Url>, + #[arg( + long, + env = "OAUTH_DISCORD_AUTH_URL", + default_value = "https://discord.com/api/oauth2/authorize?response_type=code" + )] + discord_auth_url: Option<Url>, +} |
