1212//! - Environment-based configuration loading
1313//! - Namespace directory management
1414
15- use std:: { net:: SocketAddr , path:: PathBuf } ;
15+ use std:: { net:: { IpAddr , SocketAddr } , path:: PathBuf } ;
1616
1717use getset:: Getters ;
1818use microsandbox_utils:: { env, NAMESPACES_SUBDIR } ;
1919use serde:: Deserialize ;
2020
21- use crate :: { port :: LOCALHOST_IP , MicrosandboxServerError , MicrosandboxServerResult } ;
21+ use crate :: { MicrosandboxServerError , MicrosandboxServerResult } ;
2222
2323//--------------------------------------------------------------------------------------------------
2424// Constants
@@ -45,6 +45,12 @@ pub struct Config {
4545 /// Whether to run the server in development mode
4646 dev_mode : bool ,
4747
48+ /// Host address to listen on
49+ host : IpAddr ,
50+
51+ /// Port number to listen on
52+ port : u16 ,
53+
4854 /// Address to listen on
4955 addr : SocketAddr ,
5056}
@@ -57,6 +63,7 @@ impl Config {
5763 /// Create a new configuration
5864 pub fn new (
5965 key : Option < String > ,
66+ host : String ,
6067 port : u16 ,
6168 namespace_dir : Option < PathBuf > ,
6269 dev_mode : bool ,
@@ -72,14 +79,21 @@ impl Config {
7279 }
7380 } ;
7481
75- let addr = SocketAddr :: new ( LOCALHOST_IP , port) ;
82+ // Parse host string to IpAddr
83+ let host_ip: IpAddr = host. parse ( ) . map_err ( |_| {
84+ MicrosandboxServerError :: ConfigError ( format ! ( "Invalid host address: {}" , host) )
85+ } ) ?;
86+
87+ let addr = SocketAddr :: new ( host_ip, port) ;
7688 let namespace_dir = namespace_dir
7789 . unwrap_or_else ( || env:: get_microsandbox_home_path ( ) . join ( NAMESPACES_SUBDIR ) ) ;
7890
7991 Ok ( Self {
8092 key,
8193 namespace_dir,
8294 dev_mode,
95+ host : host_ip,
96+ port,
8397 addr,
8498 } )
8599 }
0 commit comments