From 681b2c4c10bd291c1a6772a2694c6abbb62c26da Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 11 Jul 2024 19:10:04 +0100 Subject: [PATCH] refactor: split messages and handlers for server-actor --- .../server-actor/src/handlers/file_updated.rs | 21 ++++ crates/server-actor/src/handlers/mod.rs | 3 + .../src/handlers/receive_server_config.rs | 38 +++++++ .../handlers/receive_valid_server_config.rs | 39 ++++++++ crates/server-actor/src/lib.rs | 99 +------------------ crates/server-actor/src/messages.rs | 21 ++++ 6 files changed, 127 insertions(+), 94 deletions(-) create mode 100644 crates/server-actor/src/handlers/file_updated.rs create mode 100644 crates/server-actor/src/handlers/mod.rs create mode 100644 crates/server-actor/src/handlers/receive_server_config.rs create mode 100644 crates/server-actor/src/handlers/receive_valid_server_config.rs create mode 100644 crates/server-actor/src/messages.rs diff --git a/crates/server-actor/src/handlers/file_updated.rs b/crates/server-actor/src/handlers/file_updated.rs new file mode 100644 index 00000000..fc96f8d4 --- /dev/null +++ b/crates/server-actor/src/handlers/file_updated.rs @@ -0,0 +1,21 @@ +//- +use actix::prelude::*; +use git_next_config::server::ServerConfig; +use git_next_file_watcher_actor::FileUpdated; + +use crate::{messages::ReceiveServerConfig, Server}; + +impl Handler for Server { + type Result = (); + + fn handle(&mut self, _msg: FileUpdated, ctx: &mut Self::Context) -> Self::Result { + let server_config = match ServerConfig::load(&self.fs) { + Ok(server_config) => server_config, + Err(err) => { + tracing::error!("Failed to load config file. Error: {}", err); + return; + } + }; + self.do_send(ReceiveServerConfig::new(server_config), ctx); + } +} diff --git a/crates/server-actor/src/handlers/mod.rs b/crates/server-actor/src/handlers/mod.rs new file mode 100644 index 00000000..4c189e55 --- /dev/null +++ b/crates/server-actor/src/handlers/mod.rs @@ -0,0 +1,3 @@ +mod file_updated; +mod receive_server_config; +mod receive_valid_server_config; diff --git a/crates/server-actor/src/handlers/receive_server_config.rs b/crates/server-actor/src/handlers/receive_server_config.rs new file mode 100644 index 00000000..9ac9c03c --- /dev/null +++ b/crates/server-actor/src/handlers/receive_server_config.rs @@ -0,0 +1,38 @@ +use actix::prelude::*; + +use crate::{ + messages::{ReceiveServerConfig, ReceiveValidServerConfig, ValidServerConfig}, + Server, +}; + +impl Handler for Server { + type Result = (); + + #[allow(clippy::cognitive_complexity)] + fn handle(&mut self, msg: ReceiveServerConfig, ctx: &mut Self::Context) -> Self::Result { + tracing::info!("recieved server config"); + let Ok(socket_addr) = msg.http() else { + tracing::error!("Unable to parse http.addr"); + return; + }; + + let Some(server_storage) = self.server_storage(&msg) else { + tracing::error!("Server storage not available"); + return; + }; + + if msg.webhook().base_url().ends_with('/') { + tracing::error!("webhook.url must not end with a '/'"); + return; + } + + self.do_send( + ReceiveValidServerConfig::new(ValidServerConfig::new( + msg.unwrap(), + socket_addr, + server_storage, + )), + ctx, + ); + } +} diff --git a/crates/server-actor/src/handlers/receive_valid_server_config.rs b/crates/server-actor/src/handlers/receive_valid_server_config.rs new file mode 100644 index 00000000..5dba1f43 --- /dev/null +++ b/crates/server-actor/src/handlers/receive_valid_server_config.rs @@ -0,0 +1,39 @@ +use actix::prelude::*; +use git_next_webhook_actor::{AddWebhookRecipient, ShutdownWebhook, WebhookActor, WebhookRouter}; + +use crate::{ + messages::{ReceiveValidServerConfig, ValidServerConfig}, + Server, +}; + +impl Handler for Server { + type Result = (); + + fn handle(&mut self, msg: ReceiveValidServerConfig, _ctx: &mut Self::Context) -> Self::Result { + let ValidServerConfig { + server_config, + socket_address, + server_storage, + } = msg.unwrap(); + if let Some(webhook) = self.webhook.take() { + webhook.do_send(ShutdownWebhook); + } + self.generation.inc(); + // Webhook Server + tracing::info!("Starting Webhook Server..."); + let webhook_router = WebhookRouter::default().start(); + let webhook = server_config.webhook(); + // Forge Actors + for (forge_alias, forge_config) in server_config.forges() { + self.create_forge_repos(forge_config, forge_alias.clone(), &server_storage, webhook) + .into_iter() + .map(|a| self.start_actor(a)) + .map(|(repo_alias, addr)| { + AddWebhookRecipient::new(forge_alias.clone(), repo_alias, addr.recipient()) + }) + .for_each(|msg| webhook_router.do_send(msg)); + } + let webhook = WebhookActor::new(socket_address, webhook_router.recipient()).start(); + self.webhook.replace(webhook); + } +} diff --git a/crates/server-actor/src/lib.rs b/crates/server-actor/src/lib.rs index 8687a75f..d04ff5b4 100644 --- a/crates/server-actor/src/lib.rs +++ b/crates/server-actor/src/lib.rs @@ -2,41 +2,27 @@ #[cfg(test)] mod tests; +mod handlers; +mod messages; + use actix::prelude::*; -use derive_more::Constructor; -use git_next_actor_macros::message; use git_next_config as config; use git_next_config::server::{ServerConfig, ServerStorage, Webhook}; use git_next_config::{ForgeAlias, ForgeConfig, GitDir, RepoAlias, ServerRepoConfig}; -use git_next_file_watcher_actor::FileUpdated; use git_next_git::{Generation, RepoDetails}; use git_next_repo_actor::{messages::CloneRepo, RepoActor}; use git_next_webhook_actor as webhook; use kxio::{fs::FileSystem, network::Network}; use std::{ - net::SocketAddr, path::PathBuf, sync::{Arc, RwLock}, }; use tracing::{error, info}; -use webhook::{AddWebhookRecipient, ShutdownWebhook, WebhookActor, WebhookRouter}; +use webhook::WebhookActor; pub use git_next_git::repository::{factory::real as repository_factory, RepositoryFactory}; -message!(ReceiveServerConfig: ServerConfig: "Notification of newly loaded server configuration. - - -This message will prompt the `git-next` server to stop and restart all repo-actors. - -Contains the new server configuration."); - -#[derive(Clone, Debug, PartialEq, Eq, Constructor)] -pub struct ValidServerConfig { - server_config: ServerConfig, - socket_address: SocketAddr, - server_storage: ServerStorage, -} -message!(ReceiveValidServerConfig: ValidServerConfig: "Notification of validated server configuration."); +use crate::messages::ReceiveServerConfig; #[derive(Debug, derive_more::Display, derive_more::From)] pub enum Error { @@ -70,82 +56,7 @@ pub struct Server { impl Actor for Server { type Context = Context; } -impl Handler for Server { - type Result = (); - fn handle(&mut self, _msg: FileUpdated, ctx: &mut Self::Context) -> Self::Result { - let server_config = match ServerConfig::load(&self.fs) { - Ok(server_config) => server_config, - Err(err) => { - error!("Failed to load config file. Error: {}", err); - return; - } - }; - self.do_send(ReceiveServerConfig::new(server_config), ctx); - } -} -impl Handler for Server { - type Result = (); - - #[allow(clippy::cognitive_complexity)] - fn handle(&mut self, msg: ReceiveServerConfig, ctx: &mut Self::Context) -> Self::Result { - tracing::info!("recieved server config"); - let Ok(socket_addr) = msg.http() else { - error!("Unable to parse http.addr"); - return; - }; - - let Some(server_storage) = self.server_storage(&msg) else { - error!("Server storage not available"); - return; - }; - - if msg.webhook().base_url().ends_with('/') { - error!("webhook.url must not end with a '/'"); - return; - } - - self.do_send( - ReceiveValidServerConfig::new(ValidServerConfig::new( - msg.0, - socket_addr, - server_storage, - )), - ctx, - ); - } -} -impl Handler for Server { - type Result = (); - - fn handle(&mut self, msg: ReceiveValidServerConfig, _ctx: &mut Self::Context) -> Self::Result { - let ValidServerConfig { - server_config, - socket_address, - server_storage, - } = msg.0; - if let Some(webhook) = self.webhook.take() { - webhook.do_send(ShutdownWebhook); - } - self.generation.inc(); - // Webhook Server - info!("Starting Webhook Server..."); - let webhook_router = WebhookRouter::default().start(); - let webhook = server_config.webhook(); - // Forge Actors - for (forge_alias, forge_config) in server_config.forges() { - self.create_forge_repos(forge_config, forge_alias.clone(), &server_storage, webhook) - .into_iter() - .map(|a| self.start_actor(a)) - .map(|(repo_alias, addr)| { - AddWebhookRecipient::new(forge_alias.clone(), repo_alias, addr.recipient()) - }) - .for_each(|msg| webhook_router.do_send(msg)); - } - let webhook = WebhookActor::new(socket_address, webhook_router.recipient()).start(); - self.webhook.replace(webhook); - } -} impl Server { pub fn new( fs: FileSystem, diff --git a/crates/server-actor/src/messages.rs b/crates/server-actor/src/messages.rs new file mode 100644 index 00000000..c7f8f71a --- /dev/null +++ b/crates/server-actor/src/messages.rs @@ -0,0 +1,21 @@ +//- +use derive_more::Constructor; +use git_next_actor_macros::message; +use git_next_config::server::{ServerConfig, ServerStorage}; +use std::net::SocketAddr; + +// receive server config +message!(ReceiveServerConfig: ServerConfig: "Notification of newly loaded server configuration. + +This message will prompt the `git-next` server to stop and restart all repo-actors. + +Contains the new server configuration."); + +// receive valid server config +#[derive(Clone, Debug, PartialEq, Eq, Constructor)] +pub struct ValidServerConfig { + pub server_config: ServerConfig, + pub socket_address: SocketAddr, + pub server_storage: ServerStorage, +} +message!(ReceiveValidServerConfig: ValidServerConfig: "Notification of validated server configuration.");