Do proper async parallelism
This commit is contained in:
parent
f1a54d7784
commit
9f6d2d7b3b
1 changed files with 33 additions and 17 deletions
42
src/main.rs
42
src/main.rs
|
@ -6,6 +6,7 @@ use axum::{routing::get, Router};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
||||||
|
use tokio::task::JoinSet;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
|
@ -18,6 +19,27 @@ struct DokkuApp {
|
||||||
hosts: HashSet<String>,
|
hosts: HashSet<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DokkuApp {
|
||||||
|
async fn try_from_path(path: PathBuf) -> Option<Self> {
|
||||||
|
let vhost_path = path.join("VHOST");
|
||||||
|
|
||||||
|
if !vhost_path.is_file().await {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let current_vhosts = read_to_string(&vhost_path).await.unwrap();
|
||||||
|
|
||||||
|
if current_vhosts.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(DokkuApp {
|
||||||
|
name: String::from(path.file_name().unwrap().to_str().unwrap()),
|
||||||
|
hosts: current_vhosts.lines().map(String::from).collect(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn get_dokku_root() -> Option<std::path::PathBuf> {
|
fn get_dokku_root() -> Option<std::path::PathBuf> {
|
||||||
// First, check if we have a custom directory
|
// First, check if we have a custom directory
|
||||||
if let Some(dokku_root) = std::env::var_os("DOKKU_ROOT") {
|
if let Some(dokku_root) = std::env::var_os("DOKKU_ROOT") {
|
||||||
|
@ -48,28 +70,22 @@ fn get_dokku_root() -> Option<std::path::PathBuf> {
|
||||||
async fn hosts(State(state): State<AppState>) -> axum::Json<Vec<DokkuApp>> {
|
async fn hosts(State(state): State<AppState>) -> axum::Json<Vec<DokkuApp>> {
|
||||||
let mut dir_entries = state.dokku_root.read_dir().await.unwrap();
|
let mut dir_entries = state.dokku_root.read_dir().await.unwrap();
|
||||||
|
|
||||||
let mut apps: Vec<DokkuApp> = Vec::new();
|
let mut join_set = JoinSet::new();
|
||||||
|
|
||||||
while let Some(Ok(dir_entry)) = dir_entries.next().await {
|
while let Some(Ok(dir_entry)) = dir_entries.next().await {
|
||||||
let path = dir_entry.path();
|
let path = dir_entry.path();
|
||||||
|
|
||||||
let vhost_path = path.join("VHOST");
|
join_set.spawn(DokkuApp::try_from_path(path));
|
||||||
|
|
||||||
if !vhost_path.is_file().await {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let current_vhosts = read_to_string(&vhost_path).await.unwrap();
|
let mut apps: Vec<DokkuApp> = Vec::new();
|
||||||
|
|
||||||
if current_vhosts.is_empty() {
|
while let Some(res) = join_set.join_next().await {
|
||||||
continue;
|
if let Some(app) = res.unwrap() {
|
||||||
|
apps.push(app)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
apps.push(DokkuApp {
|
|
||||||
name: String::from(path.file_name().unwrap().to_str().unwrap()),
|
|
||||||
hosts: current_vhosts.lines().map(String::from).collect(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
axum::Json(apps)
|
axum::Json(apps)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue