1
0
Fork 0
mirror of https://github.com/zhaofengli/attic.git synced 2024-12-14 11:57:30 +00:00

config.rs: Add env variable for db url

This commit is contained in:
Charlotte 🦝 Delenk 2023-01-15 11:05:10 +01:00
parent 5216e06a18
commit fb77c7eb2b
No known key found for this signature in database
GPG key ID: AB2BD8DAF2E37122
2 changed files with 11 additions and 1 deletions

View file

@ -17,6 +17,7 @@ let
cat $configFile cat $configFile
export ATTIC_SERVER_TOKEN_HS256_SECRET_BASE64="dGVzdCBzZWNyZXQ=" export ATTIC_SERVER_TOKEN_HS256_SECRET_BASE64="dGVzdCBzZWNyZXQ="
export ATTIC_SERVER_DATABASE_URL="sqlite://:memory:"
${cfg.package}/bin/atticd --mode check-config -f $configFile ${cfg.package}/bin/atticd --mode check-config -f $configFile
cat <$configFile >$out cat <$configFile >$out
''; '';
@ -36,7 +37,7 @@ let
''; '';
hasLocalPostgresDB = let hasLocalPostgresDB = let
url = cfg.settings.database.url; url = cfg.settings.database.url or "";
localStrings = [ "localhost" "127.0.0.1" "/run/postgresql" ]; localStrings = [ "localhost" "127.0.0.1" "/run/postgresql" ];
hasLocalStrings = lib.any (lib.flip lib.hasInfix url) localStrings; hasLocalStrings = lib.any (lib.flip lib.hasInfix url) localStrings;
in config.services.postgresql.enable && lib.hasPrefix "postgresql://" url && hasLocalStrings; in config.services.postgresql.enable && lib.hasPrefix "postgresql://" url && hasLocalStrings;

View file

@ -28,6 +28,9 @@ const ENV_CONFIG_BASE64: &str = "ATTIC_SERVER_CONFIG_BASE64";
/// Environment variable storing the Base64-encoded HS256 JWT secret. /// Environment variable storing the Base64-encoded HS256 JWT secret.
const ENV_TOKEN_HS256_SECRET_BASE64: &str = "ATTIC_SERVER_TOKEN_HS256_SECRET_BASE64"; 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. /// Configuration for the Attic Server.
#[derive(Clone, Derivative, Deserialize)] #[derive(Clone, Derivative, Deserialize)]
#[derivative(Debug)] #[derivative(Debug)]
@ -118,6 +121,7 @@ pub struct Config {
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct DatabaseConfig { pub struct DatabaseConfig {
/// Connection URL. /// Connection URL.
#[serde(default = "load_database_url_from_env")]
pub url: String, pub url: String,
/// Whether to enable sending of periodic heartbeat queries. /// 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") 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 { impl CompressionConfig {
pub fn level(&self) -> CompressionLevel { pub fn level(&self) -> CompressionLevel {
if let Some(level) = self.level { if let Some(level) = self.level {