Files
LibreTunes/src/util/config.rs
Ethan Girouard 4b5b1209a5
Some checks failed
Push Workflows / rustfmt (push) Successful in 6s
Push Workflows / mdbook (push) Successful in 8s
Push Workflows / clippy (push) Failing after 34s
Push Workflows / docs (push) Successful in 42s
Push Workflows / build (push) Failing after 1m22s
Push Workflows / mdbook-server (push) Successful in 1m12s
Push Workflows / leptos-test (push) Successful in 1m30s
Push Workflows / test (push) Successful in 1m44s
Push Workflows / docker-build (push) Successful in 4m20s
Push Workflows / nix-build (push) Successful in 12m53s
Remove FromRequestParts for Config
2025-06-28 00:49:46 +00:00

102 lines
2.7 KiB
Rust

use axum::extract::FromRequestParts;
use clap::Parser;
use http::{request::Parts, StatusCode};
#[derive(Parser, Clone)]
#[command(version, about)]
pub struct Config {
#[clap(long, env, default_value = "assets/audio")]
/// The path to store audio files
pub audio_path: String,
#[clap(long, env, default_value = "assets/images")]
/// The path to store image files (album covers, profile pictures, playlist covers, etc.)
pub image_path: String,
#[clap(long, env)]
/// Whether to disable user signup
pub disable_signup: bool,
#[clap(long, env, required = true, conflicts_with = "database_config")]
/// The URL for the database connection.
database_url: Option<String>,
#[command(flatten)]
postgres_config: Option<PostgresConfig>,
#[clap(long, env)]
/// The URL for the Redis connection.
pub redis_url: String,
}
#[derive(clap::Args, Clone)]
#[group(id = "database_config", conflicts_with = "database_url")]
pub struct PostgresConfig {
#[arg(id = "POSTGRES_HOST", long = "postgres-host", env)]
/// The hostname of the database
pub host: String,
#[arg(id = "POSTGRES_USER", long = "postgres-user", env)]
/// The user for the database
pub user: Option<String>,
#[arg(
id = "POSTGRES_PASSWORD",
long = "postgres-password",
env,
requires = "POSTGRES_USER"
)]
/// The password for the database user
pub password: Option<String>,
#[arg(id = "POSTGRES_PORT", long = "postgres-port", env)]
/// The port for the database
pub port: Option<u16>,
#[arg(id = "POSTGRES_DB", long = "postgres-db", env)]
/// The name of the database
pub db: Option<String>,
}
impl PostgresConfig {
pub fn to_url(&self) -> String {
let mut url = "postgres://".to_string();
if let Some(user) = &self.user {
url.push_str(user);
if let Some(password) = &self.password {
url.push_str(&format!(":{password}"));
}
url.push('@');
}
url.push_str(&self.host);
if let Some(port) = self.port {
url.push_str(&format!(":{port}"));
}
if let Some(db) = &self.db {
url.push_str(&format!("/{db}"));
}
url
}
}
impl Config {
pub fn database_url(&self) -> String {
match &self.database_url {
Some(url) => url.clone(),
None => match &self.postgres_config {
Some(config) => config.to_url(),
None => panic!("Both database_url and postgres_config are missing. This error shouldn't be possible."),
},
}
}
}
pub fn load_config() -> Result<Config, clap::error::Error> {
Config::try_parse()
}