Allow usage of the compute hostname in the proxy

This commit is contained in:
Alexey Kondratov
2021-10-01 15:14:42 +03:00
committed by Alexey Kondratov
parent cf8e27a554
commit 2c99e2461a
2 changed files with 11 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
use anyhow::{bail, Result};
use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use std::net::{IpAddr, SocketAddr};
use std::net::{SocketAddr, ToSocketAddrs};
pub struct CPlaneApi {
auth_endpoint: &'static str,
@@ -8,7 +8,7 @@ pub struct CPlaneApi {
#[derive(Serialize, Deserialize, Debug)]
pub struct DatabaseInfo {
pub host: IpAddr, // TODO: allow host name here too
pub host: String,
pub port: u16,
pub dbname: String,
pub user: String,
@@ -16,8 +16,13 @@ pub struct DatabaseInfo {
}
impl DatabaseInfo {
pub fn socket_addr(&self) -> SocketAddr {
SocketAddr::new(self.host, self.port)
pub fn socket_addr(&self) -> Result<SocketAddr> {
let host_port = format!("{}:{}", self.host, self.port);
host_port
.to_socket_addrs()
.with_context(|| format!("cannot resolve {} to SocketAddr", host_port))?
.next()
.ok_or_else(|| anyhow::Error::msg("cannot resolve at least one SocketAddr"))
}
pub fn conn_string(&self) -> String {

View File

@@ -248,7 +248,7 @@ databases without opening the browser.
/// Create a TCP connection to a postgres database, authenticate with it, and receive the ReadyForQuery message
async fn connect_to_db(db_info: DatabaseInfo) -> anyhow::Result<tokio::net::TcpStream> {
let mut socket = tokio::net::TcpStream::connect(db_info.socket_addr()).await?;
let mut socket = tokio::net::TcpStream::connect(db_info.socket_addr()?).await?;
let config = db_info.conn_string().parse::<tokio_postgres::Config>()?;
let _ = config.connect_raw(&mut socket, NoTls).await?;
Ok(socket)