Skip to content

Commit

Permalink
Merge pull request #13 from DarkKirb/env-config
Browse files Browse the repository at this point in the history
Add support for reading database url from environment variable
  • Loading branch information
zhaofengli committed Jan 15, 2023
2 parents 1750c4b + fb77c7e commit 70ae61b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion nixos/atticd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
'';
Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 70ae61b

Please sign in to comment.