From a6a7ebeae16aed7b2a71491979ef4180ce68647e Mon Sep 17 00:00:00 2001 From: Daniel Sockwell Date: Fri, 10 May 2019 06:22:26 -0400 Subject: [PATCH] Add dotenv configuration --- src/.env | 5 +++++ src/main.rs | 10 +++++++++- src/receiver.rs | 6 ++++-- src/user.rs | 12 +++++++----- 4 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 src/.env diff --git a/src/.env b/src/.env new file mode 100644 index 0000000..9e3e3c8 --- /dev/null +++ b/src/.env @@ -0,0 +1,5 @@ +# Uncomment any of the variables below to customize your enviornment + +#SERVER_ADDR= +#REDIS_ADDR= +#POSTGRES_ADDR= diff --git a/src/main.rs b/src/main.rs index f5ac174..44f00dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,9 +33,12 @@ pub mod stream; pub mod timeline; pub mod user; pub mod ws; +use dotenv::dotenv; use futures::stream::Stream; use futures::Async; use receiver::Receiver; +use std::env; +use std::net::SocketAddr; use stream::StreamManager; use user::{Scope, User}; use warp::path; @@ -43,6 +46,7 @@ use warp::Filter as WarpFilter; fn main() { pretty_env_logger::init(); + dotenv().ok(); let redis_updates = StreamManager::new(Receiver::new()); let redis_updates_sse = redis_updates.blank_copy(); @@ -138,5 +142,9 @@ fn main() { }, ); - warp::serve(websocket.or(routes)).run(([127, 0, 0, 1], 4000)); + let address: SocketAddr = env::var("SERVER_ADDR") + .unwrap_or("127.0.0.1:4000".to_owned()) + .parse() + .expect("static string"); + warp::serve(websocket.or(routes)).run(address); } diff --git a/src/receiver.rs b/src/receiver.rs index 9d20515..567b30d 100644 --- a/src/receiver.rs +++ b/src/receiver.rs @@ -7,6 +7,7 @@ use log::info; use regex::Regex; use serde_json::Value; use std::collections::{HashMap, VecDeque}; +use std::env; use std::io::{Read, Write}; use std::net::TcpStream; use std::time::{Duration, Instant}; @@ -48,12 +49,13 @@ impl Default for Receiver { } impl Receiver { pub fn new() -> Self { - let pubsub_connection = TcpStream::connect("127.0.0.1:6379").expect("Can connect to Redis"); + let redis_addr = env::var("REDIS_ADDR").unwrap_or("127.0.0.1:6379".to_string()); + let pubsub_connection = TcpStream::connect(&redis_addr).expect("Can connect to Redis"); pubsub_connection .set_read_timeout(Some(Duration::from_millis(10))) .expect("Can set read timeout for Redis connection"); let secondary_redis_connection = - TcpStream::connect("127.0.0.1:6379").expect("Can connect to Redis"); + TcpStream::connect(&redis_addr).expect("Can connect to Redis"); secondary_redis_connection .set_read_timeout(Some(Duration::from_millis(10))) .expect("Can set read timeout for Redis connection"); diff --git a/src/user.rs b/src/user.rs index 9fce5bf..5a5cbb2 100644 --- a/src/user.rs +++ b/src/user.rs @@ -2,15 +2,17 @@ use crate::{any_of, query}; use log::info; use postgres; +use std::env; use warp::Filter as WarpFilter; /// (currently hardcoded to localhost) pub fn connect_to_postgres() -> postgres::Connection { - postgres::Connection::connect( - "postgres://dsock@localhost/mastodon_development", - postgres::TlsMode::None, - ) - .expect("Can connect to local Postgres") + let postgres_addr = env::var("POSTGRESS_ADDR").unwrap_or(format!( + "postgres://{}@localhost/mastodon_development", + env::var("USER").expect("User env var should exist") + )); + postgres::Connection::connect(postgres_addr, postgres::TlsMode::None) + .expect("Can connect to local Postgres") } /// The filters that can be applied to toots after they come from Redis