File tree Expand file tree Collapse file tree 1 file changed +19
-2
lines changed Expand file tree Collapse file tree 1 file changed +19
-2
lines changed Original file line number Diff line number Diff line change 1
1
extern crate redis;
2
2
3
- use self :: redis:: { Client , Commands } ;
3
+ use std:: env;
4
+
5
+ use self :: redis:: { Client , Commands , ConnectionInfo , ConnectionAddr } ;
4
6
5
7
use super :: { DataStore , DataStoreError } ;
6
8
@@ -36,7 +38,22 @@ impl DataStore for RedisStore {
36
38
37
39
impl RedisStore {
38
40
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
+
40
57
Ok ( RedisStore { client : redis_client } )
41
58
}
42
59
}
You can’t perform that action at this time.
0 commit comments