diff --git a/src/plugin/types.rs b/src/plugin/types.rs index dd14071..3c4b1d8 100644 --- a/src/plugin/types.rs +++ b/src/plugin/types.rs @@ -1,5 +1,7 @@ use serde::{Deserialize, Serialize}; +use crate::types::message::{ClientMessage, WsMessage}; + #[derive(Debug, Serialize, Deserialize)] pub struct PluginJson { pub id: String, @@ -14,9 +16,11 @@ pub struct PluginHandshake { pub id: String, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize)] #[serde(tag = "type", content = "params", rename_all = "snake_case")] -pub enum LoaderMessage {} +pub enum LoaderMessage { + Request(WsMessage), +} #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", content = "params", rename_all = "snake_case")] diff --git a/src/server.rs b/src/server.rs index eab23d4..910f7ec 100644 --- a/src/server.rs +++ b/src/server.rs @@ -8,7 +8,7 @@ use std::{ use crate::{ logger, - plugin::loader::PluginLoader, + plugin::{Plugin, loader::PluginLoader, types::LoaderMessage}, types, utils::{self, client::Client}, }; @@ -27,8 +27,8 @@ pub struct Server { pub root: PathBuf, pub config: ServerConfig, pub clients: Mutex>, + pub plugins: Mutex>, pub db: utils::database::Database, - pub plugin_loader: PluginLoader, } impl Default for ServerConfig { @@ -62,14 +62,15 @@ impl Server { root: root.to_path_buf(), config, clients: Mutex::new(HashSet::new()), - plugin_loader: PluginLoader::new(), + plugins: Mutex::new(Vec::new()), }) } pub fn run(self: &Arc) -> crate::Result<()> { // Start plugin loader + let plugin_loader = PluginLoader::new(); Self::LOGGER.info("Starting loader"); - self.plugin_loader.start_server(); + plugin_loader.start_server(); // Load plugins Self::LOGGER.info("Loading plugins"); @@ -78,7 +79,7 @@ impl Server { let path = entry.path(); if path.extension().and_then(|s| s.to_str()) == Some("json") { - self.plugin_loader.load(&path); + self.plugins.lock().unwrap().push(plugin_loader.load(&path)); } } Self::LOGGER.info("Plugins loaded"); @@ -174,14 +175,12 @@ impl Server { fn handle_client(self: &Arc, client: &Client) -> anyhow::Result<()> { // The main req/res loop - 'outer: loop { + loop { let req = client.read()?; if let Some(r) = &req { - // for p in self.plugins.lock().unwrap().iter_mut() { - // if p.on_request(r, client, self) { - // continue 'outer; - // } - // } + for p in self.plugins.lock().unwrap().iter_mut() { + p.send(LoaderMessage::Request(r.clone())); + } self.wrap_err(&client, self.call_request(r, &client))?; } diff --git a/src/types.rs b/src/types.rs index fa3b899..f6d7961 100644 --- a/src/types.rs +++ b/src/types.rs @@ -128,7 +128,7 @@ pub mod message { } /// WebSocket wrapper - #[derive(Debug, Clone)] + #[derive(Debug, Clone, Serialize)] pub enum WsMessage Deserialize<'de>> { Message(T), Binary(Vec),