@@ -27,14 +27,15 @@ pub(crate) use self::{
27
27
} ,
28
28
} ;
29
29
30
+ use home:: home_dir;
30
31
use lazy_static:: lazy_static;
31
32
32
33
use self :: util:: TestLock ;
33
34
use crate :: {
34
35
client:: options:: { ServerApi , ServerApiVersion } ,
35
36
options:: ClientOptions ,
36
37
} ;
37
- use std:: str:: FromStr ;
38
+ use std:: { fs :: read_to_string , str:: FromStr } ;
38
39
39
40
const MAX_POOL_SIZE : u32 = 100 ;
40
41
@@ -48,8 +49,7 @@ lazy_static! {
48
49
options
49
50
} ;
50
51
pub ( crate ) static ref LOCK : TestLock = TestLock :: new( ) ;
51
- pub ( crate ) static ref DEFAULT_URI : String =
52
- std:: env:: var( "MONGODB_URI" ) . unwrap_or_else( |_| "mongodb://localhost:27017" . to_string( ) ) ;
52
+ pub ( crate ) static ref DEFAULT_URI : String = get_default_uri( ) ;
53
53
pub ( crate ) static ref SERVER_API : Option <ServerApi > = match std:: env:: var( "MONGODB_API_VERSION" )
54
54
{
55
55
Ok ( server_api_version) if !server_api_version. is_empty( ) => Some ( ServerApi {
@@ -62,3 +62,16 @@ lazy_static! {
62
62
pub ( crate ) static ref SERVERLESS : bool =
63
63
matches!( std:: env:: var( "SERVERLESS" ) , Ok ( s) if s == "serverless" ) ;
64
64
}
65
+
66
+ fn get_default_uri ( ) -> String {
67
+ if let Ok ( uri) = std:: env:: var ( "MONGODB_URI" ) {
68
+ return uri;
69
+ }
70
+ if let Some ( mut home) = home_dir ( ) {
71
+ home. push ( ".mongodb_uri" ) ;
72
+ if let Ok ( uri) = read_to_string ( home) {
73
+ return uri;
74
+ }
75
+ }
76
+ return "mongodb://localhost:27017" . to_string ( ) ;
77
+ }
0 commit comments