mod client;
mod server;
use std::env;
use anyhow::Result;
use tokio::process::{Child, Command};
pub use client::*;
pub use server::*;
#[derive(Default)]
pub struct Proxy {
pub port: Option<u16>,
pub command: Option<Command>,
}
impl Proxy {
pub fn default_command() -> Option<Command> {
let mut cmd = Command::new(env::args().nth(1)?);
cmd.args(env::args().skip(2));
cmd.into()
}
pub fn port(mut self, port: u16) -> Self {
self.port = Some(port);
self
}
pub fn command(mut self, cmd: Command) -> Self {
self.command = Some(cmd);
self
}
pub async fn spawn(self) -> Result<RunningProxy> {
let port = self
.port
.or_else(|| {
env::var("AWS_LAMBDA_RUNTIME_PROXY_PORT")
.ok()
.and_then(|s| s.parse().ok())
})
.unwrap_or(3000);
let mut command = self
.command
.or_else(Self::default_command)
.ok_or_else(|| anyhow::anyhow!("Handler command is not set."))?;
command.env("AWS_LAMBDA_RUNTIME_API", format!("127.0.0.1:{}", port));
let server = MockLambdaRuntimeApiServer::bind(port).await?;
let handler = command.spawn()?;
Ok(RunningProxy { server, handler })
}
}
pub struct RunningProxy {
pub server: MockLambdaRuntimeApiServer,
pub handler: Child,
}