Removed wasm because it limits our system
This commit is contained in:
+20
-16
@@ -1,6 +1,7 @@
|
||||
use std::{
|
||||
net::TcpListener,
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
#[cfg(feature = "loader")]
|
||||
@@ -13,7 +14,7 @@ pub mod vfs;
|
||||
pub use anyhow::Result;
|
||||
use tungstenite::accept;
|
||||
|
||||
use crate::plugin::{Plugin, PluginInstance};
|
||||
use crate::plugin::Plugin;
|
||||
pub use once_cell;
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
@@ -22,7 +23,7 @@ pub struct ServerConfig {
|
||||
}
|
||||
|
||||
pub struct Server {
|
||||
plugins: Vec<PluginInstance>,
|
||||
plugins: Mutex<Vec<Box<dyn Plugin>>>,
|
||||
root: PathBuf,
|
||||
config: ServerConfig,
|
||||
}
|
||||
@@ -34,7 +35,7 @@ impl Default for ServerConfig {
|
||||
}
|
||||
|
||||
impl ServerConfig {
|
||||
pub fn build(self, root: &Path) -> Server {
|
||||
pub fn build(self, root: &Path) -> Arc<Server> {
|
||||
Server::new_config(root, self)
|
||||
}
|
||||
}
|
||||
@@ -42,29 +43,32 @@ impl ServerConfig {
|
||||
impl Server {
|
||||
logger!(LOGGER "Server");
|
||||
|
||||
pub fn new(root: &Path) -> Self {
|
||||
Self {
|
||||
plugins: Vec::new(),
|
||||
pub fn new(root: &Path) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
plugins: Mutex::new(Vec::new()),
|
||||
root: root.to_path_buf(),
|
||||
config: ServerConfig::default(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn new_config(root: &Path, config: ServerConfig) -> Self {
|
||||
Self {
|
||||
plugins: Vec::new(),
|
||||
pub fn new_config(root: &Path, config: ServerConfig) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
plugins: Mutex::new(Vec::new()),
|
||||
root: root.to_path_buf(),
|
||||
config,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> Result<()> {
|
||||
pub fn run(self: &Arc<Self>) -> Result<()> {
|
||||
// Load plugins
|
||||
#[cfg(feature = "loader")]
|
||||
loader::load_plugins(&mut self.plugins, &self.root.join("./plugins"))?;
|
||||
loader::load_plugins(
|
||||
&mut *self.plugins.lock().unwrap(),
|
||||
&self.root.join("./plugins"),
|
||||
)?;
|
||||
|
||||
// Initialize plugins
|
||||
for plugin in &mut self.plugins {
|
||||
for plugin in self.plugins.lock().unwrap().iter_mut() {
|
||||
plugin.init();
|
||||
}
|
||||
|
||||
@@ -90,7 +94,7 @@ impl Server {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_plugin(&mut self, plugin: PluginInstance) {
|
||||
self.plugins.push(plugin);
|
||||
pub fn add_plugin(self: &Arc<Self>, plugin: Box<dyn Plugin>) {
|
||||
self.plugins.lock().unwrap().push(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
+11
-22
@@ -1,41 +1,30 @@
|
||||
use std::path::Path;
|
||||
|
||||
use wasmtime::{Engine, Linker, Module, Store};
|
||||
use wasmtime_wasi::WasiCtxBuilder;
|
||||
use libloading::{Library, Symbol};
|
||||
|
||||
use crate::{PluginInstance, logger, vfs};
|
||||
use crate::{logger, plugin::Plugin, vfs};
|
||||
|
||||
logger! {
|
||||
const LOGGER "Loader"
|
||||
}
|
||||
|
||||
pub fn load_plugin(wasm_path: &Path) -> anyhow::Result<PluginInstance> {
|
||||
let engine = Engine::default();
|
||||
let mut store = Store::new(
|
||||
&engine,
|
||||
WasiCtxBuilder::new()
|
||||
.inherit_stdout()
|
||||
.inherit_stderr()
|
||||
.build_p1(),
|
||||
);
|
||||
let module = Module::from_file(&engine, wasm_path)?;
|
||||
let mut linker = Linker::new(&engine);
|
||||
|
||||
wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |s| s)?;
|
||||
|
||||
let instance = linker.instantiate(&mut store, &module)?;
|
||||
Ok(PluginInstance::Wasm(instance, store))
|
||||
pub fn load_plugin(path: &Path) -> anyhow::Result<Box<dyn Plugin>> {
|
||||
unsafe {
|
||||
LOGGER.info(format!("Loading plugin: {:?}", path));
|
||||
let lib = Library::new(path)?;
|
||||
let func: Symbol<extern "C" fn() -> Box<dyn Plugin>> = lib.get(b"load_plugin").unwrap();
|
||||
Ok(func())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_plugins(arr: &mut Vec<PluginInstance>, path: &Path) -> crate::Result<()> {
|
||||
pub fn load_plugins(arr: &mut Vec<Box<dyn Plugin>>, path: &Path) -> crate::Result<()> {
|
||||
LOGGER.info("Loading plugins");
|
||||
vfs::dir(path)?;
|
||||
if path.is_dir() {
|
||||
for entry in std::fs::read_dir(path)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.extension().and_then(|s| s.to_str()) == Some("wasm") {
|
||||
LOGGER.info(format!("Loading plugin: {:?}", path));
|
||||
if path.extension().and_then(|s| s.to_str()) == Some("dylib") {
|
||||
match load_plugin(&path) {
|
||||
Ok(plugin) => {
|
||||
arr.push(plugin);
|
||||
|
||||
+3
-9
@@ -1,15 +1,9 @@
|
||||
#[macro_export]
|
||||
macro_rules! export_plugin {
|
||||
($plugin_type:ty) => {
|
||||
static PLUGIN: std::sync::Mutex<Option<$plugin_type>> = std::sync::Mutex::new(None);
|
||||
|
||||
($p:expr) => {
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn init() {
|
||||
let mut plugin = PLUGIN.lock().unwrap();
|
||||
*plugin = Some(<$plugin_type>::default());
|
||||
if let Some(plugin) = plugin.as_mut() {
|
||||
plugin.init();
|
||||
}
|
||||
pub extern "C" fn load_plugin() -> Box<dyn Plugin> {
|
||||
$p
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+2
-3
@@ -3,9 +3,8 @@ use std::path::PathBuf;
|
||||
use voxa_server::{ServerConfig, vfs};
|
||||
|
||||
fn main() -> voxa_server::Result<()> {
|
||||
let root = PathBuf::from("./");
|
||||
let root = PathBuf::from("");
|
||||
let config: ServerConfig = vfs::read_config(&root.join("config.json"))?;
|
||||
let mut server = config.build(&root);
|
||||
server.run()?;
|
||||
config.build(&root).run()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,29 +1,3 @@
|
||||
#[cfg(feature = "loader")]
|
||||
use wasmtime::{Instance, Store};
|
||||
#[cfg(feature = "loader")]
|
||||
use wasmtime_wasi::preview1::WasiP1Ctx;
|
||||
|
||||
pub trait Plugin {
|
||||
fn init(&mut self);
|
||||
}
|
||||
|
||||
pub enum PluginInstance {
|
||||
#[cfg(feature = "loader")]
|
||||
Wasm(Instance, Store<WasiP1Ctx>),
|
||||
Static(Box<dyn Plugin>),
|
||||
}
|
||||
|
||||
impl Plugin for PluginInstance {
|
||||
fn init(&mut self) {
|
||||
match self {
|
||||
#[cfg(feature = "loader")]
|
||||
PluginInstance::Wasm(instance, store) => {
|
||||
let init = instance
|
||||
.get_typed_func::<(), ()>(&mut *store, "init")
|
||||
.unwrap();
|
||||
init.call(store, ()).unwrap();
|
||||
}
|
||||
PluginInstance::Static(plugin) => plugin.init(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user