Skip to content

Commit 2d01cf6

Browse files
authored
feat(cli): add --host support (#253)
* feat: add --host * feat: add --host to msb * chore: add constant defines
1 parent 016b58d commit 2d01cf6

File tree

8 files changed

+39
-5
lines changed

8 files changed

+39
-5
lines changed

microsandbox-cli/bin/msb/handlers.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,15 @@ pub async fn clean_subcommand(
423423
}
424424

425425
pub async fn server_start_subcommand(
426+
host: Option<String>,
426427
port: Option<u16>,
427428
namespace_dir: Option<PathBuf>,
428429
dev_mode: bool,
429430
key: Option<String>,
430431
detach: bool,
431432
reset_key: bool,
432433
) -> MicrosandboxCliResult<()> {
433-
microsandbox_server::start(key, port, namespace_dir, dev_mode, detach, reset_key).await?;
434+
microsandbox_server::start(key, host, port, namespace_dir, dev_mode, detach, reset_key).await?;
434435
Ok(())
435436
}
436437

microsandbox-cli/bin/msb/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ async fn main() -> MicrosandboxCliResult<()> {
219219
}
220220
Some(MicrosandboxSubcommand::Server { subcommand }) => match subcommand {
221221
ServerSubcommand::Start {
222+
host,
222223
port,
223224
namespace_dir,
224225
dev_mode,
@@ -227,6 +228,7 @@ async fn main() -> MicrosandboxCliResult<()> {
227228
reset_key,
228229
} => {
229230
handlers::server_start_subcommand(
231+
host,
230232
port,
231233
namespace_dir,
232234
dev_mode,

microsandbox-cli/bin/msbserver.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub async fn main() -> MicrosandboxCliResult<()> {
3535
// Create configuration from arguments
3636
let config = Arc::new(Config::new(
3737
args.key,
38+
args.host,
3839
args.port,
3940
args.namespace_dir.clone(),
4041
args.dev_mode,

microsandbox-cli/lib/args/msb.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,10 @@ pub enum MicrosandboxSubcommand {
608608
pub enum ServerSubcommand {
609609
/// Start the sandbox server which is also an MCP server
610610
Start {
611+
/// Host to listen on
612+
#[arg(long)]
613+
host: Option<String>,
614+
611615
/// Port to listen on
612616
#[arg(long)]
613617
port: Option<u16>,

microsandbox-cli/lib/args/msbserver.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22

33
use clap::Parser;
4-
use microsandbox_utils::DEFAULT_SERVER_PORT;
4+
use microsandbox_utils::{DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT};
55

66
use crate::styles;
77

@@ -17,6 +17,10 @@ pub struct MsbserverArgs {
1717
#[arg(short = 'k', long = "key")]
1818
pub key: Option<String>,
1919

20+
/// Host address to listen on
21+
#[arg(long, default_value = DEFAULT_SERVER_HOST)]
22+
pub host: String,
23+
2024
/// Port number to listen on
2125
#[arg(long, default_value_t = DEFAULT_SERVER_PORT)]
2226
pub port: u16,

microsandbox-server/lib/config.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
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

1717
use getset::Getters;
1818
use microsandbox_utils::{env, NAMESPACES_SUBDIR};
1919
use 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
}

microsandbox-server/lib/management.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub struct Claims {
7171
/// Start the sandbox server
7272
pub async fn start(
7373
key: Option<String>,
74+
host: Option<String>,
7475
port: Option<u16>,
7576
namespace_dir: Option<PathBuf>,
7677
dev_mode: bool,
@@ -143,6 +144,10 @@ pub async fn start(
143144
command.arg("--dev");
144145
}
145146

147+
if let Some(host) = host {
148+
command.arg("--host").arg(host);
149+
}
150+
146151
if let Some(port) = port {
147152
command.arg("--port").arg(port.to_string());
148153
}

microsandbox-utils/lib/defaults.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ pub const DEFAULT_WORKDIR: &str = "/";
6969
/// The default namespace for the sandbox server.
7070
pub const DEFAULT_SERVER_NAMESPACE: &str = "default";
7171

72+
/// The default localhost address.
73+
pub const DEFAULT_SERVER_HOST: &str = "127.0.0.1";
74+
7275
/// The default microsandbox-server port.
7376
pub const DEFAULT_SERVER_PORT: u16 = 5555;
7477

0 commit comments

Comments
 (0)