use anyhow::{Context, Result};
pub async fn run_gateway(
listen: String,
data_dir: String,
tls_cert: Option<String>,
tls_key: Option<String>,
) -> Result<()> {
use ipfrs_interface::tls::TlsConfig;
use ipfrs_interface::{Gateway, GatewayConfig};
use ipfrs_storage::BlockStoreConfig;
let storage_config = BlockStoreConfig {
path: std::path::PathBuf::from(&data_dir).join("blocks"),
cache_size: 100 * 1024 * 1024, };
let tls_config = match (tls_cert, tls_key) {
(Some(cert), Some(key)) => {
if !std::path::Path::new(&cert).exists() {
anyhow::bail!("TLS certificate file not found: {}", cert);
}
if !std::path::Path::new(&key).exists() {
anyhow::bail!("TLS private key file not found: {}", key);
}
Some(TlsConfig {
cert_path: cert.into(),
key_path: key.into(),
})
}
(None, None) => None,
(Some(_), None) => {
anyhow::bail!("TLS certificate provided but private key is missing (use --tls-key)");
}
(None, Some(_)) => {
anyhow::bail!("TLS private key provided but certificate is missing (use --tls-cert)");
}
};
let config = GatewayConfig {
listen_addr: listen.clone(),
storage_config,
tls_config: tls_config.clone(),
compression_config: Default::default(),
};
let protocol = if tls_config.is_some() {
"HTTPS"
} else {
"HTTP"
};
eprintln!("Starting {} gateway on {}", protocol, listen);
if tls_config.is_some() {
eprintln!("TLS enabled - using secure connections");
}
let gateway = Gateway::new(config).context("Failed to create gateway")?;
gateway
.start()
.await
.context("Failed to start gateway server")?;
Ok(())
}