use boxlite::runtime::{BoxliteRuntime, BoxOptions};
use boxlite::runtime::options::{RootfsSpec, SecurityOptions, VolumeSpec};
use boxlite::BoxCommand;
use futures::StreamExt;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize runtime
let runtime = BoxliteRuntime::with_defaults()?;
// Configure box
let options = BoxOptions {
cpus: Some(2),
memory_mib: Some(1024),
rootfs: RootfsSpec::Image("python:3.11-slim".to_string()),
volumes: vec![
VolumeSpec {
host_path: "/home/user/code".to_string(),
guest_path: "/app".to_string(),
read_only: true,
},
],
security: SecurityOptions::standard(),
..Default::default()
};
// Create and name the box
let litebox = runtime.create(options, Some("python-sandbox".to_string())).await?;
println!("Created box: {}", litebox.id());
// Run Python code
let cmd = BoxCommand::new("python3")
.args(["-c", "import sys; print(f'Python {sys.version}')"])
.timeout(Duration::from_secs(30))
.working_dir("/app");
let mut run_handle = litebox.run(cmd).await?;
// Stream output
if let Some(mut stdout) = run_handle.stdout() {
while let Some(line) = stdout.next().await {
println!("{}", line);
}
}
// Check result
let result = run_handle.wait().await?;
if !result.success() {
eprintln!("Command failed with exit code: {}", result.exit_code);
}
// Check metrics
let metrics = litebox.metrics().await?;
if let Some(boot_ms) = metrics.guest_boot_duration_ms() {
println!("Boot time: {}ms", boot_ms);
}
// Cleanup
litebox.stop().await?;
Ok(())
}