refactor: Improve async
This commit is contained in:
+2
-5
@@ -4,17 +4,15 @@ 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<Output = Option<(Vec<u8>, &'static str)>> {
|
||||
) -> Option<(Vec<u8>, &'static str)> {
|
||||
let re = regex!(r"^talos/(?<cluster>.+)/(?<node>.+)\.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();
|
||||
@@ -55,4 +53,3 @@ pub fn get_file(
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-4
@@ -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 {
|
||||
tftp::serve(ip, move |filename, ip| async move {
|
||||
get_file(filename, format!("http://{ip}:{port}"))
|
||||
.await
|
||||
.map(|(file, _)| file)
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
}
|
||||
|
||||
+3
-3
@@ -37,7 +37,7 @@ pub async fn handle_connection<F, Fut>(
|
||||
callback: F,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
F: Fn(&str, IpAddr) -> Fut,
|
||||
F: Fn(String, IpAddr) -> Fut,
|
||||
Fut: Future<Output = Option<Vec<u8>>>,
|
||||
{
|
||||
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<F, Fut>(ip: IpAddr, callback: F) -> Result<!, Error>
|
||||
where
|
||||
F: Fn(&str, IpAddr) -> Fut + Clone + Send + 'static,
|
||||
F: Fn(String, IpAddr) -> Fut + Clone + Send + 'static,
|
||||
Fut: Future<Output = Option<Vec<u8>>> + Send + 'static,
|
||||
{
|
||||
let socket = PktInfoUdpSocket::new(Domain::IPV4)?;
|
||||
|
||||
Reference in New Issue
Block a user