Skip to content

Commit 2c99e24

Browse files
committed
Allow usage of the compute hostname in the proxy
1 parent cf8e27a commit 2c99e24

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

proxy/src/cplane_api.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
use anyhow::{bail, Result};
1+
use anyhow::{bail, Context, Result};
22
use serde::{Deserialize, Serialize};
3-
use std::net::{IpAddr, SocketAddr};
3+
use std::net::{SocketAddr, ToSocketAddrs};
44

55
pub struct CPlaneApi {
66
auth_endpoint: &'static str,
77
}
88

99
#[derive(Serialize, Deserialize, Debug)]
1010
pub struct DatabaseInfo {
11-
pub host: IpAddr, // TODO: allow host name here too
11+
pub host: String,
1212
pub port: u16,
1313
pub dbname: String,
1414
pub user: String,
1515
pub password: String,
1616
}
1717

1818
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"))
2126
}
2227

2328
pub fn conn_string(&self) -> String {

proxy/src/proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ databases without opening the browser.
248248

249249
/// Create a TCP connection to a postgres database, authenticate with it, and receive the ReadyForQuery message
250250
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?;
252252
let config = db_info.conn_string().parse::<tokio_postgres::Config>()?;
253253
let _ = config.connect_raw(&mut socket, NoTls).await?;
254254
Ok(socket)

0 commit comments

Comments
 (0)