Better structure
This commit is contained in:
+25
-12
@@ -5,11 +5,16 @@ use wasmtime_wasi::preview1::WasiP1Ctx;
|
|||||||
|
|
||||||
#[cfg(feature = "loader")]
|
#[cfg(feature = "loader")]
|
||||||
pub mod loader;
|
pub mod loader;
|
||||||
|
pub mod macros;
|
||||||
|
|
||||||
|
pub use anyhow::Result;
|
||||||
|
|
||||||
pub trait PluginApi {
|
pub trait PluginApi {
|
||||||
fn init(&mut self);
|
fn init(&mut self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct ServerConfig {}
|
||||||
|
|
||||||
pub enum PluginInstance {
|
pub enum PluginInstance {
|
||||||
#[cfg(feature = "loader")]
|
#[cfg(feature = "loader")]
|
||||||
Wasm(Instance, Store<WasiP1Ctx>),
|
Wasm(Instance, Store<WasiP1Ctx>),
|
||||||
@@ -31,18 +36,26 @@ impl PluginApi for PluginInstance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
impl ServerConfig {
|
||||||
macro_rules! export_plugin {
|
pub fn start(&self) -> Result<()> {
|
||||||
($plugin_type:ty) => {
|
let mut plugins: Vec<PluginInstance> = Vec::new();
|
||||||
static PLUGIN: std::sync::Mutex<Option<$plugin_type>> = std::sync::Mutex::new(None);
|
#[cfg(feature = "loader")]
|
||||||
|
loader::load_plugins(&mut plugins, std::path::Path::new("./plugins"));
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
self.start_with(plugins)
|
||||||
pub extern "C" fn init() {
|
}
|
||||||
let mut plugin = PLUGIN.lock().unwrap();
|
|
||||||
*plugin = Some(<$plugin_type>::default());
|
pub fn start_with(&self, mut plugins: Vec<PluginInstance>) -> Result<()> {
|
||||||
if let Some(plugin) = plugin.as_mut() {
|
for plugin in &mut plugins {
|
||||||
plugin.init();
|
plugin.init();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ServerConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#[macro_export]
|
||||||
|
macro_rules! export_plugin {
|
||||||
|
($plugin_type:ty) => {
|
||||||
|
static PLUGIN: std::sync::Mutex<Option<$plugin_type>> = std::sync::Mutex::new(None);
|
||||||
|
|
||||||
|
#[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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
+2
-10
@@ -1,12 +1,4 @@
|
|||||||
use voxa_server::PluginApi;
|
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let mut plugins = Vec::new();
|
let config = voxa_server::ServerConfig::default();
|
||||||
voxa_server::loader::load_plugins(&mut plugins, std::path::Path::new("./plugins"));
|
config.start()
|
||||||
|
|
||||||
for plugin in &mut plugins {
|
|
||||||
plugin.init();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib", "lib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
voxa-server = { path = "../", default-features = false }
|
voxa-server = { path = "../", default-features = false }
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
use voxa_server::PluginInstance;
|
||||||
|
|
||||||
|
fn main() -> voxa_server::Result<()> {
|
||||||
|
let config = voxa_server::ServerConfig::default();
|
||||||
|
let plugin = test_plugin::MyPlugin::default();
|
||||||
|
config.start_with(vec![PluginInstance::StatiC(Box::new(plugin))])
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user