Improved structure
This commit is contained in:
+17
-15
@@ -5,23 +5,16 @@ use std::{
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
pub mod client;
|
||||
pub mod database;
|
||||
#[cfg(feature = "loader")]
|
||||
pub mod loader;
|
||||
pub mod logger;
|
||||
pub mod macros;
|
||||
pub mod plugin;
|
||||
pub mod types;
|
||||
pub mod vfs;
|
||||
pub mod utils;
|
||||
|
||||
pub use anyhow::Result;
|
||||
pub use tungstenite;
|
||||
|
||||
use crate::{
|
||||
client::Client,
|
||||
plugin::DynPlugin,
|
||||
types::{ClientMessage, WsMessage},
|
||||
utils::client::Client,
|
||||
utils::plugin::DynPlugin,
|
||||
};
|
||||
pub use once_cell;
|
||||
|
||||
@@ -37,7 +30,7 @@ pub struct Server {
|
||||
config: ServerConfig,
|
||||
plugins: Mutex<Vec<DynPlugin>>,
|
||||
clients: Mutex<HashSet<Client>>,
|
||||
pub db: database::Database,
|
||||
pub db: utils::database::Database,
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
@@ -64,7 +57,7 @@ impl Server {
|
||||
|
||||
pub fn new_config(root: &Path, config: ServerConfig) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
db: database::Database::new(&config).unwrap(),
|
||||
db: utils::database::Database::new(&config).unwrap(),
|
||||
plugins: Mutex::new(Vec::new()),
|
||||
root: root.to_path_buf(),
|
||||
config,
|
||||
@@ -75,7 +68,7 @@ impl Server {
|
||||
pub fn run(self: &Arc<Self>) -> Result<()> {
|
||||
// Load plugins
|
||||
#[cfg(feature = "loader")]
|
||||
loader::load_plugins(
|
||||
utils::loader::load_plugins(
|
||||
&mut *self.plugins.lock().unwrap(),
|
||||
&self.root.join("./plugins"),
|
||||
)?;
|
||||
@@ -122,8 +115,17 @@ impl Server {
|
||||
self.clients.lock().unwrap().insert(client.clone());
|
||||
|
||||
// The main req/res loop
|
||||
loop {
|
||||
match client.read()? {
|
||||
'outer: 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match req {
|
||||
Some(WsMessage::Message(req)) => match req {
|
||||
ClientMessage::SendMessage {
|
||||
channel_id,
|
||||
|
||||
+7
-7
@@ -2,7 +2,7 @@
|
||||
macro_rules! export_plugin {
|
||||
($p:expr) => {
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn load_plugin() -> $crate::plugin::DynPlugin {
|
||||
pub extern "C" fn load_plugin() -> $crate::utils::plugin::DynPlugin {
|
||||
$p
|
||||
}
|
||||
};
|
||||
@@ -11,20 +11,20 @@ macro_rules! export_plugin {
|
||||
#[macro_export]
|
||||
macro_rules! logger {
|
||||
(const $i:ident $name:expr) => {
|
||||
const $i: $crate::once_cell::sync::Lazy<$crate::logger::Logger> =
|
||||
$crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name));
|
||||
pub const $i: $crate::once_cell::sync::Lazy<$crate::utils::logger::Logger> =
|
||||
$crate::once_cell::sync::Lazy::new(|| $crate::utils::logger::Logger::new($name));
|
||||
};
|
||||
|
||||
($i:ident $name:expr) => {
|
||||
const $i: $crate::once_cell::sync::Lazy<$crate::logger::Logger> =
|
||||
$crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name));
|
||||
pub const $i: $crate::once_cell::sync::Lazy<$crate::utils::logger::Logger> =
|
||||
$crate::once_cell::sync::Lazy::new(|| $crate::utils::logger::Logger::new($name));
|
||||
};
|
||||
|
||||
(const $name:expr) => {
|
||||
$crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name))
|
||||
$crate::once_cell::sync::Lazy::new(|| $crate::utils::logger::Logger::new($name))
|
||||
};
|
||||
|
||||
($name:expr) => {
|
||||
$crate::logger::Logger::new($name)
|
||||
$crate::logger::utils::Logger::new($name)
|
||||
};
|
||||
}
|
||||
|
||||
+15
-3
@@ -29,11 +29,17 @@ pub enum ClientMessage {
|
||||
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
|
||||
pub enum ServerMessage {
|
||||
/// Successful authentication
|
||||
Authenticated { user_id: String },
|
||||
Authenticated {
|
||||
user_id: String,
|
||||
},
|
||||
|
||||
/// Error responses
|
||||
Error(),
|
||||
|
||||
TempMessage {
|
||||
message: String,
|
||||
},
|
||||
|
||||
/// A new message in a channel
|
||||
MessageCreate(data::Message),
|
||||
|
||||
@@ -47,10 +53,16 @@ pub enum ServerMessage {
|
||||
},
|
||||
|
||||
/// Presence updates
|
||||
PresenceUpdate { user_id: String, status: String },
|
||||
PresenceUpdate {
|
||||
user_id: String,
|
||||
status: String,
|
||||
},
|
||||
|
||||
/// Typing indicator
|
||||
Typing { user_id: String, channel_id: String },
|
||||
Typing {
|
||||
user_id: String,
|
||||
channel_id: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// WebSocket wrapper
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::path::Path;
|
||||
|
||||
use libloading::{Library, Symbol};
|
||||
|
||||
use crate::{logger, plugin::DynPlugin, vfs};
|
||||
use crate::{logger, utils::plugin::DynPlugin, utils::vfs};
|
||||
|
||||
logger! {
|
||||
const LOGGER "Loader"
|
||||
@@ -0,0 +1,7 @@
|
||||
pub mod client;
|
||||
pub mod database;
|
||||
#[cfg(feature = "loader")]
|
||||
pub mod loader;
|
||||
pub mod logger;
|
||||
pub mod plugin;
|
||||
pub mod vfs;
|
||||
@@ -3,6 +3,7 @@ use std::sync::Arc;
|
||||
use crate::{
|
||||
Server,
|
||||
types::{ClientMessage, WsMessage},
|
||||
utils::client::Client,
|
||||
};
|
||||
|
||||
pub type DynPlugin = Box<dyn Plugin + Send + Sync>;
|
||||
@@ -10,7 +11,12 @@ pub type DynPlugin = Box<dyn Plugin + Send + Sync>;
|
||||
pub trait Plugin {
|
||||
fn init(&mut self, server: &Arc<Server>);
|
||||
#[allow(unused_variables)]
|
||||
fn on_request(&mut self, msg: &WsMessage<ClientMessage>, server: &Arc<Server>) -> bool {
|
||||
fn on_request(
|
||||
&mut self,
|
||||
req: &WsMessage<ClientMessage>,
|
||||
client: &Client,
|
||||
server: &Arc<Server>,
|
||||
) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user