File tree Expand file tree Collapse file tree 2 files changed +11
-6
lines changed Expand file tree Collapse file tree 2 files changed +11
-6
lines changed Original file line number Diff line number Diff line change 1
- use anyhow:: { bail, Result } ;
1
+ use anyhow:: { bail, Context , Result } ;
2
2
use serde:: { Deserialize , Serialize } ;
3
- use std:: net:: { IpAddr , SocketAddr } ;
3
+ use std:: net:: { SocketAddr , ToSocketAddrs } ;
4
4
5
5
pub struct CPlaneApi {
6
6
auth_endpoint : & ' static str ,
7
7
}
8
8
9
9
#[ derive( Serialize , Deserialize , Debug ) ]
10
10
pub struct DatabaseInfo {
11
- pub host : IpAddr , // TODO: allow host name here too
11
+ pub host : String ,
12
12
pub port : u16 ,
13
13
pub dbname : String ,
14
14
pub user : String ,
15
15
pub password : String ,
16
16
}
17
17
18
18
impl DatabaseInfo {
19
- pub fn socket_addr ( & self ) -> SocketAddr {
20
- SocketAddr :: new ( self . host , self . port )
19
+ pub fn socket_addr ( & self ) -> Result < SocketAddr > {
20
+ let host_port = format ! ( "{}:{}" , self . host, self . port) ;
21
+ host_port
22
+ . to_socket_addrs ( )
23
+ . with_context ( || format ! ( "cannot resolve {} to SocketAddr" , host_port) ) ?
24
+ . next ( )
25
+ . ok_or_else ( || anyhow:: Error :: msg ( "cannot resolve at least one SocketAddr" ) )
21
26
}
22
27
23
28
pub fn conn_string ( & self ) -> String {
Original file line number Diff line number Diff line change @@ -248,7 +248,7 @@ databases without opening the browser.
248
248
249
249
/// Create a TCP connection to a postgres database, authenticate with it, and receive the ReadyForQuery message
250
250
async fn connect_to_db ( db_info : DatabaseInfo ) -> anyhow:: Result < tokio:: net:: TcpStream > {
251
- let mut socket = tokio:: net:: TcpStream :: connect ( db_info. socket_addr ( ) ) . await ?;
251
+ let mut socket = tokio:: net:: TcpStream :: connect ( db_info. socket_addr ( ) ? ) . await ?;
252
252
let config = db_info. conn_string ( ) . parse :: < tokio_postgres:: Config > ( ) ?;
253
253
let _ = config. connect_raw ( & mut socket, NoTls ) . await ?;
254
254
Ok ( socket)
You can’t perform that action at this time.
0 commit comments