Skip to content

Commit a0543f4

Browse files
committed
Read redis configuration from environment variables
1 parent e01d007 commit a0543f4

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/datastore/redis_store.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
extern crate redis;
22

3-
use self::redis::{Client, Commands};
3+
use std::env;
4+
5+
use self::redis::{Client, Commands, ConnectionInfo, ConnectionAddr};
46

57
use super::{DataStore, DataStoreError};
68

@@ -36,7 +38,22 @@ impl DataStore for RedisStore {
3638

3739
impl RedisStore {
3840
pub fn new() -> Result<RedisStore, DataStoreError> {
39-
let redis_client = try!(Client::open("redis://127.0.0.1/"));
41+
// Read env variables
42+
let redis_host: String = env::var("REDIS_HOST").unwrap_or("127.0.0.1".to_string());
43+
let redis_port: u16 = env::var("REDIS_PORT").unwrap_or("6379".to_string()).parse().unwrap_or(6379);
44+
let redis_db: i64 = env::var("REDIS_DB").unwrap_or("0".to_string()).parse().unwrap_or(0);
45+
46+
// Build connection info
47+
info!("Connecting to redis://{}:{}/{}...", &redis_host, &redis_port, &redis_db);
48+
let connection_info = ConnectionInfo {
49+
addr: Box::new(ConnectionAddr::Tcp(redis_host, redis_port)),
50+
db: redis_db,
51+
passwd: None,
52+
};
53+
54+
// Create redis client
55+
let redis_client = try!(Client::open(connection_info));
56+
4057
Ok(RedisStore { client: redis_client })
4158
}
4259
}

0 commit comments

Comments
 (0)