From 161da3f4561fbfafcdad8512f50ed88ab0e708d3 Mon Sep 17 00:00:00 2001 From: Daniel Sockwell Date: Tue, 27 Aug 2019 18:26:37 -0400 Subject: [PATCH] Add aditional Postgres config options This commit adds several aditional Postgres config options, such as PORT, USER, DB_NAME and similar. It also relocates the .env file so that it will be picked up even if flodgat isn't run from the src/ directory. --- .env | 25 ++++++++++++++++++++++ src/.env | 11 ---------- src/config.rs | 57 +++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 .env delete mode 100644 src/.env diff --git a/.env b/.env new file mode 100644 index 0000000..9665504 --- /dev/null +++ b/.env @@ -0,0 +1,25 @@ +# Uncomment any of the variables below to customize your enviornment + +#POSTGRES_ADDR= +#REDIS_ADDR= +#SERVER_ADDR= +#SSE_UPDATE_INTERVAL= +#WS_UPDATE_INTERVAL= +#REDIS_POLL_INTERVAL= + +# +# Postgres settings +# +#DB_HOST= +#DB_USER= +#DB_NAME= +#DB_PASS= +#DB_PORT= +# *note*: `DATABASE_URL` is an alternative to the variables above; if +# it is set, it will overwrite all other Postgres env variables +# schema: postgresql://[USER[:PASSWORD]@][HOST][:PORT][/DB_NAME] +#DATABASE_URL= + + +#Possible values for the log level are error, warn, info, debug, trace +RUST_LOG=warn \ No newline at end of file diff --git a/src/.env b/src/.env deleted file mode 100644 index 6fd4480..0000000 --- a/src/.env +++ /dev/null @@ -1,11 +0,0 @@ -# Uncomment any of the variables below to customize your enviornment - -#POSTGRES_ADDR= -#REDIS_ADDR= -#SERVER_ADDR= -#SSE_UPDATE_INTERVAL= -#WS_UPDATE_INTERVAL= -#REDIS_POLL_INTERVAL= - -#Possible values for the log level are eror, warn, info, debug, trace -RUST_LOG=warn \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 70511ad..be3dc07 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,7 +9,10 @@ use std::{env, net, time}; const CORS_ALLOWED_METHODS: [&str; 2] = ["GET", "OPTIONS"]; const CORS_ALLOWED_HEADERS: [&str; 3] = ["Authorization", "Accept", "Cache-Control"]; -const DEFAULT_POSTGRES_ADDR: &str = "postgres://@localhost/mastodon_development"; +const DEFAULT_DB_HOST: &str = "localhost"; +const DEFAULT_DB_USER: &str = "postgres"; +const DEFAULT_DB_NAME: &str = "mastodon_development"; +const DEFAULT_DB_PORT: &str = "5432"; const DEFAULT_REDIS_ADDR: &str = "127.0.0.1:6379"; const DEFAULT_SERVER_ADDR: &str = "127.0.0.1:4000"; @@ -17,23 +20,47 @@ const DEFAULT_SSE_UPDATE_INTERVAL: u64 = 100; const DEFAULT_WS_UPDATE_INTERVAL: u64 = 100; const DEFAULT_REDIS_POLL_INTERVAL: u64 = 100; +fn env_var_or_default(var: &str, default_var: &str) -> String { + env::var(var) + .unwrap_or_else(|_| { + warn!( + "No {} env variable set for Postgres. Using default value: {}", + var, default_var + ); + default_var.to_string() + }) + .to_string() +} + lazy_static! { - static ref POSTGRES_ADDR: String = env::var("POSTGRES_ADDR").unwrap_or_else(|_| { - warn!("No POSTGRES_ADDR env variable set; using default postgres address."); - match &env::var("USER") { - Err(_) => { - let addr = DEFAULT_POSTGRES_ADDR.replace("@", format!("{}@", "postgres").as_str()); - warn!("No USER env variable set; using default `postgres` user.\n Using postgres address: {}\n", addr); - addr - }, - Ok(user) => { - let addr = DEFAULT_POSTGRES_ADDR.replace("@", format!("{}@", user).as_str()); - warn!("Connecting to postgres with current user.\n Using postgres address: {}\n", addr); - addr + + static ref POSTGRES_ADDR: String = match &env::var("POSTGRESS_ADDR") { + Ok(url) => { + warn!("DATABASE_URL env variable set. Trying to connect to Postgres with that URL instead of any values set in DB_HOST, DB_USER, DB_NAME, DB_PASS, or DB_PORT."); + url.to_string() + } + Err(_) => { + let user = &env::var("DB_USER").unwrap_or_else(|_| { + match &env::var("USER") { + Err(_) => env_var_or_default("DB_USER", DEFAULT_DB_USER), + Ok(user) => env_var_or_default("DB_USER", user) + } + }); + let host = &env::var("DB_HOST").unwrap_or_else(|_| env_var_or_default("DB_HOST", DEFAULT_DB_HOST)); + let db_name = &env::var("DB_NAME").unwrap_or_else(|_| env_var_or_default("DB_NAME", DEFAULT_DB_NAME)); + let port = &env::var("DB_PORT").unwrap_or_else(|_| env_var_or_default("DB_PORT", DEFAULT_DB_PORT)); + + match &env::var("DB_PASS") { + Ok(password) => format!("postgres://{user}:{password}@{host}:{port}/{db_name}", + user = user, password = password, host = host, port = port, db_name = db_name), + Err(_) => { + warn!("No DB_PASSWORD set. Attempting to connect to Postgres without a password. (This is correct if you are using the `ident` method.)"); + format!("postgres://{user}@{host}:{port}/{db_name}", + user = user, host = host, port = port, db_name = db_name) + }, } } - - }); + }; static ref REDIS_ADDR: String = env::var("REDIS_ADDR").unwrap_or_else(|_| DEFAULT_REDIS_ADDR.to_owned());