Working plugin system (not really)

This commit is contained in:
2025-12-12 09:47:59 +01:00
parent cc620511a9
commit 62c9328143
3 changed files with 17 additions and 14 deletions
+6 -2
View File
@@ -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<ClientMessage>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
+10 -11
View File
@@ -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<HashSet<Client>>,
pub plugins: Mutex<Vec<Plugin>>,
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<Self>) -> 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<Self>, 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))?;
}
+1 -1
View File
@@ -128,7 +128,7 @@ pub mod message {
}
/// WebSocket wrapper
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub enum WsMessage<T: Serialize + for<'de> Deserialize<'de>> {
Message(T),
Binary(Vec<u8>),