Add dotenv configuration

This commit is contained in:
Daniel Sockwell 2019-05-10 06:22:26 -04:00
parent 1e9358f95d
commit a6a7ebeae1
4 changed files with 25 additions and 8 deletions

5
src/.env Normal file
View File

@ -0,0 +1,5 @@
# Uncomment any of the variables below to customize your enviornment
#SERVER_ADDR=
#REDIS_ADDR=
#POSTGRES_ADDR=

View File

@ -33,9 +33,12 @@ pub mod stream;
pub mod timeline; pub mod timeline;
pub mod user; pub mod user;
pub mod ws; pub mod ws;
use dotenv::dotenv;
use futures::stream::Stream; use futures::stream::Stream;
use futures::Async; use futures::Async;
use receiver::Receiver; use receiver::Receiver;
use std::env;
use std::net::SocketAddr;
use stream::StreamManager; use stream::StreamManager;
use user::{Scope, User}; use user::{Scope, User};
use warp::path; use warp::path;
@ -43,6 +46,7 @@ use warp::Filter as WarpFilter;
fn main() { fn main() {
pretty_env_logger::init(); pretty_env_logger::init();
dotenv().ok();
let redis_updates = StreamManager::new(Receiver::new()); let redis_updates = StreamManager::new(Receiver::new());
let redis_updates_sse = redis_updates.blank_copy(); 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);
} }

View File

@ -7,6 +7,7 @@ use log::info;
use regex::Regex; use regex::Regex;
use serde_json::Value; use serde_json::Value;
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use std::env;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::net::TcpStream; use std::net::TcpStream;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -48,12 +49,13 @@ impl Default for Receiver {
} }
impl Receiver { impl Receiver {
pub fn new() -> Self { 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 pubsub_connection
.set_read_timeout(Some(Duration::from_millis(10))) .set_read_timeout(Some(Duration::from_millis(10)))
.expect("Can set read timeout for Redis connection"); .expect("Can set read timeout for Redis connection");
let secondary_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 secondary_redis_connection
.set_read_timeout(Some(Duration::from_millis(10))) .set_read_timeout(Some(Duration::from_millis(10)))
.expect("Can set read timeout for Redis connection"); .expect("Can set read timeout for Redis connection");

View File

@ -2,15 +2,17 @@
use crate::{any_of, query}; use crate::{any_of, query};
use log::info; use log::info;
use postgres; use postgres;
use std::env;
use warp::Filter as WarpFilter; use warp::Filter as WarpFilter;
/// (currently hardcoded to localhost) /// (currently hardcoded to localhost)
pub fn connect_to_postgres() -> postgres::Connection { pub fn connect_to_postgres() -> postgres::Connection {
postgres::Connection::connect( let postgres_addr = env::var("POSTGRESS_ADDR").unwrap_or(format!(
"postgres://dsock@localhost/mastodon_development", "postgres://{}@localhost/mastodon_development",
postgres::TlsMode::None, env::var("USER").expect("User env var should exist")
) ));
.expect("Can connect to local Postgres") 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 /// The filters that can be applied to toots after they come from Redis