Working plugin system (not really)
This commit is contained in:
+6
-2
@@ -1,5 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::types::message::{ClientMessage, WsMessage};
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct PluginJson {
|
pub struct PluginJson {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
@@ -14,9 +16,11 @@ pub struct PluginHandshake {
|
|||||||
pub id: String,
|
pub id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
|
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
|
||||||
pub enum LoaderMessage {}
|
pub enum LoaderMessage {
|
||||||
|
Request(WsMessage<ClientMessage>),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
|
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
|
||||||
|
|||||||
+10
-11
@@ -8,7 +8,7 @@ use std::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
logger,
|
logger,
|
||||||
plugin::loader::PluginLoader,
|
plugin::{Plugin, loader::PluginLoader, types::LoaderMessage},
|
||||||
types,
|
types,
|
||||||
utils::{self, client::Client},
|
utils::{self, client::Client},
|
||||||
};
|
};
|
||||||
@@ -27,8 +27,8 @@ pub struct Server {
|
|||||||
pub root: PathBuf,
|
pub root: PathBuf,
|
||||||
pub config: ServerConfig,
|
pub config: ServerConfig,
|
||||||
pub clients: Mutex<HashSet<Client>>,
|
pub clients: Mutex<HashSet<Client>>,
|
||||||
|
pub plugins: Mutex<Vec<Plugin>>,
|
||||||
pub db: utils::database::Database,
|
pub db: utils::database::Database,
|
||||||
pub plugin_loader: PluginLoader,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ServerConfig {
|
impl Default for ServerConfig {
|
||||||
@@ -62,14 +62,15 @@ impl Server {
|
|||||||
root: root.to_path_buf(),
|
root: root.to_path_buf(),
|
||||||
config,
|
config,
|
||||||
clients: Mutex::new(HashSet::new()),
|
clients: Mutex::new(HashSet::new()),
|
||||||
plugin_loader: PluginLoader::new(),
|
plugins: Mutex::new(Vec::new()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(self: &Arc<Self>) -> crate::Result<()> {
|
pub fn run(self: &Arc<Self>) -> crate::Result<()> {
|
||||||
// Start plugin loader
|
// Start plugin loader
|
||||||
|
let plugin_loader = PluginLoader::new();
|
||||||
Self::LOGGER.info("Starting loader");
|
Self::LOGGER.info("Starting loader");
|
||||||
self.plugin_loader.start_server();
|
plugin_loader.start_server();
|
||||||
|
|
||||||
// Load plugins
|
// Load plugins
|
||||||
Self::LOGGER.info("Loading plugins");
|
Self::LOGGER.info("Loading plugins");
|
||||||
@@ -78,7 +79,7 @@ impl Server {
|
|||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
|
|
||||||
if path.extension().and_then(|s| s.to_str()) == Some("json") {
|
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");
|
Self::LOGGER.info("Plugins loaded");
|
||||||
@@ -174,14 +175,12 @@ impl Server {
|
|||||||
|
|
||||||
fn handle_client(self: &Arc<Self>, client: &Client) -> anyhow::Result<()> {
|
fn handle_client(self: &Arc<Self>, client: &Client) -> anyhow::Result<()> {
|
||||||
// The main req/res loop
|
// The main req/res loop
|
||||||
'outer: loop {
|
loop {
|
||||||
let req = client.read()?;
|
let req = client.read()?;
|
||||||
if let Some(r) = &req {
|
if let Some(r) = &req {
|
||||||
// for p in self.plugins.lock().unwrap().iter_mut() {
|
for p in self.plugins.lock().unwrap().iter_mut() {
|
||||||
// if p.on_request(r, client, self) {
|
p.send(LoaderMessage::Request(r.clone()));
|
||||||
// continue 'outer;
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
self.wrap_err(&client, self.call_request(r, &client))?;
|
self.wrap_err(&client, self.call_request(r, &client))?;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -128,7 +128,7 @@ pub mod message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// WebSocket wrapper
|
/// WebSocket wrapper
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum WsMessage<T: Serialize + for<'de> Deserialize<'de>> {
|
pub enum WsMessage<T: Serialize + for<'de> Deserialize<'de>> {
|
||||||
Message(T),
|
Message(T),
|
||||||
Binary(Vec<u8>),
|
Binary(Vec<u8>),
|
||||||
|
|||||||
Reference in New Issue
Block a user