Better structure

This commit is contained in:
2025-08-29 13:52:04 +02:00
parent 68bf864a52
commit 6149cb7744
5 changed files with 50 additions and 23 deletions
+25 -12
View File
@@ -5,11 +5,16 @@ use wasmtime_wasi::preview1::WasiP1Ctx;
#[cfg(feature = "loader")]
pub mod loader;
pub mod macros;
pub use anyhow::Result;
pub trait PluginApi {
fn init(&mut self);
}
pub struct ServerConfig {}
pub enum PluginInstance {
#[cfg(feature = "loader")]
Wasm(Instance, Store<WasiP1Ctx>),
@@ -31,18 +36,26 @@ impl PluginApi for PluginInstance {
}
}
#[macro_export]
macro_rules! export_plugin {
($plugin_type:ty) => {
static PLUGIN: std::sync::Mutex<Option<$plugin_type>> = std::sync::Mutex::new(None);
impl ServerConfig {
pub fn start(&self) -> Result<()> {
let mut plugins: Vec<PluginInstance> = Vec::new();
#[cfg(feature = "loader")]
loader::load_plugins(&mut plugins, std::path::Path::new("./plugins"));
#[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();
}
self.start_with(plugins)
}
pub fn start_with(&self, mut plugins: Vec<PluginInstance>) -> Result<()> {
for plugin in &mut plugins {
plugin.init();
}
};
Ok(())
}
}
impl Default for ServerConfig {
fn default() -> Self {
Self {}
}
}
+15
View File
@@ -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
View File
@@ -1,12 +1,4 @@
use voxa_server::PluginApi;
fn main() -> anyhow::Result<()> {
let mut plugins = Vec::new();
voxa_server::loader::load_plugins(&mut plugins, std::path::Path::new("./plugins"));
for plugin in &mut plugins {
plugin.init();
}
Ok(())
let config = voxa_server::ServerConfig::default();
config.start()
}
+1 -1
View File
@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "lib"]
[dependencies]
voxa-server = { path = "../", default-features = false }
+7
View File
@@ -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))])
}