From 7e08640743fa68bf924f65e54405646c36ca9cf3 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 9 Apr 2024 07:20:13 +0100 Subject: [PATCH] refactor(server): reduce complexity starting server --- src/server/config.rs | 10 ++++- src/server/mod.rs | 92 ++++++++++++++++++++++++++------------------ 2 files changed, 63 insertions(+), 39 deletions(-) diff --git a/src/server/config.rs b/src/server/config.rs index e582524..6afc258 100644 --- a/src/server/config.rs +++ b/src/server/config.rs @@ -42,7 +42,7 @@ pub struct RepoBranches { dev: String, } -#[derive(Debug, PartialEq, Eq, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Deserialize)] pub struct Forge { forge_type: ForgeType, hostname: String, @@ -53,6 +53,7 @@ pub struct Forge { repos: HashMap, } impl Forge { + #[allow(dead_code)] pub const fn forge_type(&self) -> &ForgeType { &self.forge_type } @@ -75,8 +76,13 @@ impl Forge { .map(|(name, repo)| (RepoName(name.clone()), repo)) } } +impl Display for Forge { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{} - {}@{}", self.forge_type, self.user, self.hostname) + } +} -#[derive(Debug, PartialEq, Eq, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Deserialize)] pub struct Repo { repo: String, branch: String, diff --git a/src/server/mod.rs b/src/server/mod.rs index ddd894c..47ff3ca 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -9,9 +9,11 @@ use tracing::{error, info}; use crate::{ filesystem::FileSystem, - server::config::{ForgeName, RepoName}, + server::config::{Forge, ForgeName, RepoName}, }; +use self::{actors::repo::RepoActor, config::Repo}; + pub fn init(fs: FileSystem) { let file_name = "git-next-server.toml"; let path = PathBuf::from(file_name); @@ -43,51 +45,67 @@ pub fn start(fs: FileSystem) { return; } }; + info!("Config loaded"); - info!("Loaded config"); - let mut actors: Vec<(ForgeName, RepoName, actors::repo::RepoActor)> = vec![]; - config.forges().for_each(|(forge_name, forge)| { - let span = tracing::info_span!("Forge", %forge_name); - let _guard = span.enter(); - info!("Forge: {}", forge_name); - info!("Forge Type: {}", forge.forge_type()); - info!("Hostname: {}", forge.hostname()); - info!("User: {}", forge.user()); - forge.repos().for_each(|(repo_name, repo)| { - let span = tracing::info_span!("Repo", %repo_name, %repo); - let _guard = span.enter(); - info!("Creating Repo"); - let actor = actors::repo::RepoActor::new(config::RepoDetails::new( - &repo_name, - repo, - &forge_name, - forge, - )); - actors.push((forge_name.clone(), repo_name, actor)); - info!("Created Repo"); - }); - }); - info!("Sending StartRepo to all actors..."); + let actors = config + .forges() + .flat_map(|(forge_name, forge)| create_forge_repos(forge, forge_name)) + .collect::>(); actix::System::new().block_on(async { - let mut addresses: Vec> = vec![]; - actors.into_iter().for_each(|actor| { - let (forge_name, repo_name, actor) = actor; - let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_name); - let _guard = span.enter(); - info!("Starting actor"); - let addr = actor.start(); - info!("Sending StartRepo to actor"); - addr.do_send(actors::repo::StartRepo); - addresses.push(addr); - info!("Sent StartRepo to actor"); - }); + let addresses = actors.into_iter().map(start_actor).collect::>(); let _ = actix_rt::signal::ctrl_c().await; info!("Ctrl-C received, shutting down..."); drop(addresses); }); } +fn create_forge_repos( + forge: &Forge, + forge_name: ForgeName, +) -> Vec<(ForgeName, RepoName, RepoActor)> { + let forge = forge.clone(); + let span = tracing::info_span!("Forge", %forge_name, %forge); + let _guard = span.enter(); + info!("Creating Forge"); + forge + .repos() + .map(create_actor(forge_name, forge.clone())) + .collect::>() +} + +fn create_actor( + forge_name: ForgeName, + forge: config::Forge, +) -> impl Fn((RepoName, &Repo)) -> (ForgeName, RepoName, RepoActor) { + move |(repo_name, repo)| { + let span = tracing::info_span!("Repo", %repo_name, %repo); + let _guard = span.enter(); + info!("Creating Repo"); + let actor = actors::repo::RepoActor::new(config::RepoDetails::new( + &repo_name, + repo, + &forge_name, + &forge, + )); + info!("Created Repo"); + (forge_name.clone(), repo_name, actor) + } +} + +fn start_actor( + actor: (ForgeName, RepoName, actors::repo::RepoActor), +) -> Addr { + let (forge_name, repo_name, actor) = actor; + let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_name); + let _guard = span.enter(); + info!("Starting"); + let addr = actor.start(); + addr.do_send(actors::repo::StartRepo); + info!("Started"); + addr +} + pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> { use tracing_subscriber::prelude::*;