@@ -20,7 +20,7 @@ use sqlx::any::AnyConnectOptions;
2020use std:: convert:: { TryFrom , TryInto } ;
2121use std:: env:: var;
2222use std:: fmt:: { Debug , Display , Formatter } ;
23- use std:: net:: { IpAddr , Ipv6Addr , SocketAddr } ;
23+ use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr , SocketAddr , TcpListener } ;
2424use std:: path:: { Path , PathBuf } ;
2525use std:: str:: FromStr ;
2626
@@ -165,6 +165,15 @@ impl Display for Bind {
165165 }
166166}
167167
168+ fn default_addr ( ) -> IpAddr {
169+ let v6 = IpAddr :: V6 ( Ipv6Addr :: UNSPECIFIED ) ;
170+ if TcpListener :: bind ( ( v6, 0 ) ) . is_ok ( ) {
171+ v6
172+ } else {
173+ IpAddr :: V4 ( Ipv4Addr :: UNSPECIFIED )
174+ }
175+ }
176+
168177impl TryFrom < PartialConfig > for Config {
169178 type Error = Error ;
170179
@@ -183,7 +192,7 @@ impl TryFrom<PartialConfig> for Config {
183192 let bind = match config. socket {
184193 Some ( socket) => Bind :: Unix ( socket, socket_permissions) ,
185194 None => {
186- let ip = config. bind . unwrap_or ( IpAddr :: V6 ( Ipv6Addr :: UNSPECIFIED ) ) ;
195+ let ip = config. bind . unwrap_or_else ( default_addr ) ;
187196 let port = config. port . unwrap_or ( 7867 ) ;
188197 Bind :: Tcp ( ( ip, port) . into ( ) )
189198 }
@@ -192,7 +201,7 @@ impl TryFrom<PartialConfig> for Config {
192201 let metrics_bind = match ( config. metrics_socket , config. metrics_port ) {
193202 ( Some ( socket) , _) => Some ( Bind :: Unix ( socket, socket_permissions) ) ,
194203 ( None , Some ( port) ) => {
195- let ip = config. bind . unwrap_or ( IpAddr :: V6 ( Ipv6Addr :: UNSPECIFIED ) ) ;
204+ let ip = config. bind . unwrap_or_else ( default_addr ) ;
196205 Some ( Bind :: Tcp ( ( ip, port) . into ( ) ) )
197206 }
198207 _ => None ,
0 commit comments