From fb77c7eb2b3dbe652a60d7d11f3cfd72dbd9a0f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charlotte=20=F0=9F=A6=9D=20Delenk?= Date: Sun, 15 Jan 2023 11:05:10 +0100 Subject: [PATCH] config.rs: Add env variable for db url --- nixos/atticd.nix | 3 ++- server/src/config.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/nixos/atticd.nix b/nixos/atticd.nix index 2a52ccf..9a32a75 100644 --- a/nixos/atticd.nix +++ b/nixos/atticd.nix @@ -17,6 +17,7 @@ let cat $configFile export ATTIC_SERVER_TOKEN_HS256_SECRET_BASE64="dGVzdCBzZWNyZXQ=" + export ATTIC_SERVER_DATABASE_URL="sqlite://:memory:" ${cfg.package}/bin/atticd --mode check-config -f $configFile cat <$configFile >$out ''; @@ -36,7 +37,7 @@ let ''; hasLocalPostgresDB = let - url = cfg.settings.database.url; + url = cfg.settings.database.url or ""; localStrings = [ "localhost" "127.0.0.1" "/run/postgresql" ]; hasLocalStrings = lib.any (lib.flip lib.hasInfix url) localStrings; in config.services.postgresql.enable && lib.hasPrefix "postgresql://" url && hasLocalStrings; diff --git a/server/src/config.rs b/server/src/config.rs index 3de159f..5ee6f51 100644 --- a/server/src/config.rs +++ b/server/src/config.rs @@ -28,6 +28,9 @@ const ENV_CONFIG_BASE64: &str = "ATTIC_SERVER_CONFIG_BASE64"; /// Environment variable storing the Base64-encoded HS256 JWT secret. const ENV_TOKEN_HS256_SECRET_BASE64: &str = "ATTIC_SERVER_TOKEN_HS256_SECRET_BASE64"; +/// Environment variable storing the database connection string. +const ENV_DATABASE_URL: &str = "ATTIC_SERVER_DATABASE_URL"; + /// Configuration for the Attic Server. #[derive(Clone, Derivative, Deserialize)] #[derivative(Debug)] @@ -118,6 +121,7 @@ pub struct Config { #[derive(Debug, Clone, Deserialize)] pub struct DatabaseConfig { /// Connection URL. + #[serde(default = "load_database_url_from_env")] pub url: String, /// Whether to enable sending of periodic heartbeat queries. @@ -242,6 +246,11 @@ fn load_token_hs256_secret_from_env() -> HS256Key { decode_token_hs256_secret_base64(&s).expect("Failed to load as decoding key") } +fn load_database_url_from_env() -> String { + env::var(ENV_DATABASE_URL) + .expect("Database URL must be specified in either database.url or the ATTIC_SERVER_DATABASE_URL environment.") +} + impl CompressionConfig { pub fn level(&self) -> CompressionLevel { if let Some(level) = self.level {