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:
parent
5216e06a18
commit
fb77c7eb2b
2 changed files with 11 additions and 1 deletions
|
@ -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;
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue