Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 136 additions & 31 deletions crates/edgezero-adapter-axum/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::process::Command;

Expand Down Expand Up @@ -139,11 +140,12 @@ fn deploy(_extra_args: &[String]) -> Result<(), String> {
Err("Axum adapter does not define a deploy command. Extend your workspace manifest with one if needed.".into())
}

#[derive(Debug)]
struct AxumProject {
crate_dir: PathBuf,
cargo_manifest: PathBuf,
crate_name: String,
port: u16,
addr: SocketAddr,
}

fn locate_project() -> Result<AxumProject, String> {
Expand All @@ -155,8 +157,8 @@ fn locate_project() -> Result<AxumProject, String> {
fn run_cargo(project: &AxumProject, subcommand: &str, extra_args: &[String]) -> Result<(), String> {
let display = project.crate_dir.display();
println!(
"[edgezero] Axum {subcommand} ({}) in {} (port: {})",
project.crate_name, display, project.port
"[edgezero] Axum {subcommand} ({}) in {} ({})",
project.crate_name, display, project.addr
);
let mut command = Command::new("cargo");
command.arg(subcommand);
Expand All @@ -169,6 +171,8 @@ fn run_cargo(project: &AxumProject, subcommand: &str, extra_args: &[String]) ->
);
command.args(extra_args);
command.current_dir(&project.crate_dir);
command.env("EDGEZERO_HOST", project.addr.ip().to_string());
command.env("EDGEZERO_PORT", project.addr.port().to_string());
let status = command
.status()
.map_err(|err| format!("failed to run cargo {subcommand}: {err}"))?;
Expand Down Expand Up @@ -215,6 +219,16 @@ fn find_axum_manifest(start: &Path) -> Result<PathBuf, String> {
}

fn read_axum_project(manifest: &Path) -> Result<AxumProject, String> {
let env_host = std::env::var("EDGEZERO_HOST").ok();
let env_port = std::env::var("EDGEZERO_PORT").ok();
read_axum_project_with_env(manifest, env_host.as_deref(), env_port.as_deref())
}

fn read_axum_project_with_env(
manifest: &Path,
env_host: Option<&str>,
env_port: Option<&str>,
) -> Result<AxumProject, String> {
let contents = fs::read_to_string(manifest)
.map_err(|err| format!("failed to read {}: {err}", manifest.display()))?;
let value: Value = toml::from_str(&contents)
Expand Down Expand Up @@ -255,24 +269,30 @@ fn read_axum_project(manifest: &Path) -> Result<AxumProject, String> {
})
});

let port = match adapter.get("port").and_then(Value::as_integer) {
Some(value) => {
if !(1..=u16::MAX as i64).contains(&value) {
return Err(format!(
"adapter.port in {} must be between 1 and 65535",
manifest.display()
));
}
value as u16
}
None => 8787,
let config_host = adapter.get("host").and_then(Value::as_str);
let config_port = match adapter.get("port").and_then(Value::as_integer) {
Some(value) => Some(u16::try_from(value).map_err(|_| {
format!(
"adapter.port in {} must be between 1 and 65535",
manifest.display()
)
})?),
None => None,
};

let addr = edgezero_core::addr::resolve_bind_addr(
env_host,
env_port,
config_host,
config_port,
)
.map_err(|e| format!("{e} (in {})", manifest.display()))?;

Ok(AxumProject {
crate_dir,
cargo_manifest,
crate_name,
port,
addr,
})
}

Expand All @@ -297,11 +317,12 @@ mod tests {
)
.unwrap();

let project = read_axum_project(&root.join("axum.toml")).expect("project");
let project = read_axum_project_with_env(&root.join("axum.toml"), None, None).expect("project");
assert_eq!(project.crate_name, "demo");
assert_eq!(project.crate_dir, root);
assert_eq!(project.cargo_manifest, root.join("Cargo.toml"));
assert_eq!(project.port, 8787);
assert_eq!(project.addr.port(), edgezero_core::addr::DEFAULT_PORT);
assert_eq!(project.addr.ip(), edgezero_core::addr::DEFAULT_HOST);
}

#[test]
Expand Down Expand Up @@ -336,8 +357,8 @@ mod tests {
)
.unwrap();

let project = read_axum_project(&root.join("axum.toml")).expect("project");
assert_eq!(project.port, 4001);
let project = read_axum_project_with_env(&root.join("axum.toml"), None, None).expect("project");
assert_eq!(project.addr.port(), 4001);
}

#[test]
Expand All @@ -355,7 +376,7 @@ mod tests {
)
.unwrap();

let result = read_axum_project(&root.join("axum.toml"));
let result = read_axum_project_with_env(&root.join("axum.toml"), None, None);
match result {
Ok(_) => panic!("expected error"),
Err(e) => assert!(e.contains("must be between 1 and 65535")),
Expand All @@ -377,7 +398,7 @@ mod tests {
)
.unwrap();

let result = read_axum_project(&root.join("axum.toml"));
let result = read_axum_project_with_env(&root.join("axum.toml"), None, None);
assert!(result.is_err());
}

Expand All @@ -396,7 +417,7 @@ mod tests {
)
.unwrap();

let result = read_axum_project(&root.join("axum.toml"));
let result = read_axum_project_with_env(&root.join("axum.toml"), None, None);
assert!(result.is_err());
}

Expand All @@ -411,7 +432,7 @@ mod tests {
)
.unwrap();

let result = read_axum_project(&root.join("axum.toml"));
let result = read_axum_project_with_env(&root.join("axum.toml"), None, None);
match result {
Ok(_) => panic!("expected error"),
Err(e) => assert!(e.contains("adapter table missing")),
Expand All @@ -429,7 +450,7 @@ mod tests {
)
.unwrap();

let result = read_axum_project(&root.join("axum.toml"));
let result = read_axum_project_with_env(&root.join("axum.toml"), None, None);
match result {
Ok(_) => panic!("expected error"),
Err(e) => assert!(e.contains("crate_dir missing")),
Expand All @@ -449,7 +470,7 @@ mod tests {
.unwrap();
// No Cargo.toml in subdir

let result = read_axum_project(&root.join("axum.toml"));
let result = read_axum_project_with_env(&root.join("axum.toml"), None, None);
match result {
Ok(_) => panic!("expected error"),
Err(e) => assert!(e.contains("Cargo.toml missing")),
Expand All @@ -468,7 +489,7 @@ mod tests {
)
.unwrap();

let project = read_axum_project(&root.join("axum.toml")).expect("project");
let project = read_axum_project_with_env(&root.join("axum.toml"), None, None).expect("project");
assert_eq!(project.crate_name, "my-package");
}

Expand All @@ -489,7 +510,7 @@ mod tests {
)
.unwrap();

let project = read_axum_project(&root.join("axum.toml")).expect("project");
let project = read_axum_project_with_env(&root.join("axum.toml"), None, None).expect("project");
assert_eq!(project.crate_name, "my-adapter");
assert_eq!(project.crate_dir, adapter_dir);
}
Expand All @@ -509,8 +530,8 @@ mod tests {
)
.unwrap();

let project = read_axum_project(&root.join("axum.toml")).expect("project");
assert_eq!(project.port, 65535);
let project = read_axum_project_with_env(&root.join("axum.toml"), None, None).expect("project");
assert_eq!(project.addr.port(), 65535);
}

#[test]
Expand All @@ -528,8 +549,66 @@ mod tests {
)
.unwrap();

let project = read_axum_project(&root.join("axum.toml")).expect("project");
assert_eq!(project.port, 1);
let project = read_axum_project_with_env(&root.join("axum.toml"), None, None).expect("project");
assert_eq!(project.addr.port(), 1);
}

#[test]
fn read_axum_project_defaults_host_to_localhost() {
let dir = tempdir().unwrap();
let root = dir.path();
fs::write(
root.join("axum.toml"),
"[adapter]\ncrate = \"demo\"\ncrate_dir = \".\"\n",
)
.unwrap();
fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
)
.unwrap();

let project = read_axum_project_with_env(&root.join("axum.toml"), None, None).expect("project");
assert_eq!(project.addr.ip(), edgezero_core::addr::DEFAULT_HOST);
}

#[test]
fn read_axum_project_uses_custom_host() {
let dir = tempdir().unwrap();
let root = dir.path();
fs::write(
root.join("axum.toml"),
"[adapter]\ncrate = \"demo\"\ncrate_dir = \".\"\nhost = \"0.0.0.0\"\n",
)
.unwrap();
fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
)
.unwrap();

let project = read_axum_project_with_env(&root.join("axum.toml"), None, None).expect("project");
assert_eq!(project.addr.ip(), std::net::IpAddr::from([0, 0, 0, 0]));
}

#[test]
fn read_axum_project_rejects_invalid_host() {
let dir = tempdir().unwrap();
let root = dir.path();
fs::write(
root.join("axum.toml"),
"[adapter]\ncrate = \"demo\"\ncrate_dir = \".\"\nhost = \"not-an-ip\"\n",
)
.unwrap();
fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
)
.unwrap();

let result = read_axum_project_with_env(&root.join("axum.toml"), None, None);
assert!(result.is_err());
assert!(result.unwrap_err().contains("valid IP address"));
}

#[test]
Expand Down Expand Up @@ -605,6 +684,32 @@ mod tests {
assert_eq!(AXUM_ADAPTER.name(), "axum");
}

#[test]
fn read_axum_project_env_overrides_config() {
let dir = tempdir().unwrap();
let root = dir.path();
fs::write(
root.join("axum.toml"),
"[adapter]\ncrate = \"demo\"\ncrate_dir = \".\"\nhost = \"127.0.0.1\"\nport = 3000\n",
)
.unwrap();
fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
)
.unwrap();

let result = read_axum_project_with_env(
&root.join("axum.toml"),
Some("0.0.0.0"),
Some("9999"),
);

let project = result.expect("project");
assert_eq!(project.addr.ip(), std::net::IpAddr::from([0, 0, 0, 0]));
assert_eq!(project.addr.port(), 9999);
}

#[test]
fn blueprint_has_correct_id() {
assert_eq!(AXUM_BLUEPRINT.id, "axum");
Expand Down
Loading