diff --git a/src/file.rs b/src/file.rs index 63dec16..3860acc 100644 --- a/src/file.rs +++ b/src/file.rs @@ -4,55 +4,52 @@ use ipxe::IPXE_DIR; use minijinja::{Environment, context}; use regex::regex; -// TODO: Make this a proper async function -pub fn get_file( +pub async fn get_file( filename: impl ToString, http_url: impl ToString, -) -> impl Future, &'static str)>> { +) -> Option<(Vec, &'static str)> { let re = regex!(r"^talos/(?.+)/(?.+)\.ya?ml$"); let filename: String = filename.to_string(); let re = re.clone(); - async move { - const IPXE_PATTERN: &str = "ipxe/"; - if filename == format!("{IPXE_PATTERN}boot.ipxe") { - let mut env = Environment::new(); + const IPXE_PATTERN: &str = "ipxe/"; + if filename == format!("{IPXE_PATTERN}boot.ipxe") { + let mut env = Environment::new(); - configure_environment(&mut env); + configure_environment(&mut env); - let template = env - .template_from_str(include_str!("./boot.ipxe")) - .expect("Template should be valid"); + let template = env + .template_from_str(include_str!("./boot.ipxe")) + .expect("Template should be valid"); - let clusters = Cluster::get_all().await; - let content = template - .render(context! { - clusters => clusters.values().collect::>(), - httpUrl => http_url.to_string() - }) - .unwrap(); + let clusters = Cluster::get_all().await; + let content = template + .render(context! { + clusters => clusters.values().collect::>(), + httpUrl => http_url.to_string() + }) + .unwrap(); - Some((content.into(), "text/plain; charset=utf-8")) - } else if let Some(filename) = filename.strip_prefix(IPXE_PATTERN) { - Some(( - IPXE_DIR.get_file(filename)?.contents().to_owned(), - "application/octet-stream", - )) - } else if let Some(caps) = re.captures(&filename) { - let cluster = &caps["cluster"]; - let node = &caps["node"]; + Some((content.into(), "text/plain; charset=utf-8")) + } else if let Some(filename) = filename.strip_prefix(IPXE_PATTERN) { + Some(( + IPXE_DIR.get_file(filename)?.contents().to_owned(), + "application/octet-stream", + )) + } else if let Some(caps) = re.captures(&filename) { + let cluster = &caps["cluster"]; + let node = &caps["node"]; - let clusters = Cluster::get_all().await; - let cluster = clusters.get(cluster)?; + let clusters = Cluster::get_all().await; + let cluster = clusters.get(cluster)?; - let node = cluster.node(node)?; + let node = cluster.node(node)?; - let config = node.config(cluster).await; + let config = node.config(cluster).await; - Some((config.into(), "application/yaml; charset=utf-8")) - } else { - None - } + Some((config.into(), "application/yaml; charset=utf-8")) + } else { + None } } diff --git a/src/main.rs b/src/main.rs index 8a0fb13..03627bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,13 +114,10 @@ async fn main() -> Result<(), Error> { axum::serve(listener, app).await.unwrap(); }); - tftp::serve(ip, move |filename, ip| { - let filename = filename.to_owned(); - async move { - get_file(filename, format!("http://{ip}:{port}")) - .await - .map(|(file, _)| file) - } + tftp::serve(ip, move |filename, ip| async move { + get_file(filename, format!("http://{ip}:{port}")) + .await + .map(|(file, _)| file) }) .await?; } diff --git a/src/tftp/server.rs b/src/tftp/server.rs index caff956..026c1eb 100644 --- a/src/tftp/server.rs +++ b/src/tftp/server.rs @@ -37,7 +37,7 @@ pub async fn handle_connection( callback: F, ) -> Result<(), Error> where - F: Fn(&str, IpAddr) -> Fut, + F: Fn(String, IpAddr) -> Fut, Fut: Future>>, { let src = info.addr_src; @@ -59,7 +59,7 @@ where let filename = request.filename(); println!("{src}: {filename} [{:?}]", request.mode()); - let Some(data) = callback(filename, info.addr_dst).await else { + let Some(data) = callback(filename.to_string(), info.addr_dst).await else { Packet::error(ErrorCode::FileNotFound, "Unknown file") .send_to(&socket, &src) .await?; @@ -152,7 +152,7 @@ where pub async fn serve(ip: IpAddr, callback: F) -> Result where - F: Fn(&str, IpAddr) -> Fut + Clone + Send + 'static, + F: Fn(String, IpAddr) -> Fut + Clone + Send + 'static, Fut: Future>> + Send + 'static, { let socket = PktInfoUdpSocket::new(Domain::IPV4)?;