Better logging with plugin fix
This commit is contained in:
+1
-4
@@ -13,6 +13,7 @@ pub mod vfs;
|
||||
pub use anyhow::Result;
|
||||
|
||||
use crate::plugin::{Plugin, PluginInstance};
|
||||
pub use once_cell;
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct ServerConfig {
|
||||
@@ -71,10 +72,6 @@ impl Server {
|
||||
let listener = TcpListener::bind(format!("0.0.0.0:{}", self.config.port));
|
||||
self.logger
|
||||
.info(format!("Server listening 0.0.0.0:{}", self.config.port));
|
||||
self.logger
|
||||
.warn(format!("Server listening 0.0.0.0:{}", self.config.port));
|
||||
self.logger
|
||||
.error(format!("Server listening 0.0.0.0:{}", self.config.port));
|
||||
|
||||
for stream in listener?.incoming() {
|
||||
match stream {
|
||||
|
||||
@@ -28,6 +28,7 @@ pub fn load_plugin(wasm_path: &Path) -> anyhow::Result<PluginInstance> {
|
||||
}
|
||||
|
||||
pub fn load_plugins(arr: &mut Vec<PluginInstance>, path: &Path) -> crate::Result<()> {
|
||||
LOGGER.info("Loading plugins");
|
||||
vfs::dir(path)?;
|
||||
if path.is_dir() {
|
||||
for entry in std::fs::read_dir(path)? {
|
||||
|
||||
+5
-5
@@ -17,17 +17,17 @@ macro_rules! export_plugin {
|
||||
#[macro_export]
|
||||
macro_rules! logger {
|
||||
(const $i:ident $name:expr) => {
|
||||
const $i: once_cell::sync::Lazy<$crate::logger::Logger> =
|
||||
once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name));
|
||||
const $i: $crate::once_cell::sync::Lazy<$crate::logger::Logger> =
|
||||
$crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name));
|
||||
};
|
||||
|
||||
($i:ident $name:expr) => {
|
||||
const $i: once_cell::sync::Lazy<$crate::logger::Logger> =
|
||||
once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name));
|
||||
const $i: $crate::once_cell::sync::Lazy<$crate::logger::Logger> =
|
||||
$crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name));
|
||||
};
|
||||
|
||||
(const $name:expr) => {
|
||||
once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name))
|
||||
$crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name))
|
||||
};
|
||||
|
||||
($name:expr) => {
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@ use std::path::PathBuf;
|
||||
|
||||
use voxa_server::{ServerConfig, vfs};
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let root = PathBuf::new();
|
||||
fn main() -> voxa_server::Result<()> {
|
||||
let root = PathBuf::from("./");
|
||||
let config: ServerConfig = vfs::read_config(&root.join("config.json"))?;
|
||||
let mut server = config.build(&root);
|
||||
server.run()?;
|
||||
|
||||
+19
-15
@@ -1,9 +1,16 @@
|
||||
use std::{fs, path::Path};
|
||||
|
||||
logger! {
|
||||
const LOGGER "File"
|
||||
}
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::logger;
|
||||
|
||||
pub fn dir(path: &Path) -> crate::Result<()> {
|
||||
if !path.exists() {
|
||||
LOGGER.info(format!("Directory {path:?} does not exist, creating it"));
|
||||
fs::create_dir_all(path)?;
|
||||
}
|
||||
Ok(())
|
||||
@@ -11,11 +18,10 @@ pub fn dir(path: &Path) -> crate::Result<()> {
|
||||
|
||||
pub fn read(path: &Path, default_content: &str) -> crate::Result<String> {
|
||||
if !path.exists() {
|
||||
dir(path.parent().ok_or(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidFilename,
|
||||
"File doesn't have a parent assigned, example: `config/config.json`",
|
||||
))?)?;
|
||||
fs::write(path, default_content)?;
|
||||
LOGGER.info(format!(
|
||||
"File {path:?} does not exist, creating it with default contents"
|
||||
));
|
||||
write(path, default_content)?;
|
||||
return Ok(default_content.to_string());
|
||||
}
|
||||
|
||||
@@ -24,11 +30,10 @@ pub fn read(path: &Path, default_content: &str) -> crate::Result<String> {
|
||||
|
||||
pub fn read_bytes<'a>(path: &Path, default_content: Vec<u8>) -> crate::Result<Vec<u8>> {
|
||||
if !path.exists() {
|
||||
dir(path.parent().ok_or(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidFilename,
|
||||
"File doesn't have a parent assigned, example: `config/config.json`",
|
||||
))?)?;
|
||||
fs::write(path, &default_content)?;
|
||||
LOGGER.info(format!(
|
||||
"File {path:?} does not exist, creating it with default contents"
|
||||
));
|
||||
write_bytes(path, &default_content)?;
|
||||
return Ok(default_content);
|
||||
}
|
||||
|
||||
@@ -39,12 +44,11 @@ pub fn read_config<T: Default + Serialize + for<'de> Deserialize<'de>>(
|
||||
path: &Path,
|
||||
) -> crate::Result<T> {
|
||||
if !path.exists() {
|
||||
dir(path.parent().ok_or(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidFilename,
|
||||
"File doesn't have a parent assigned, example: `config/config.json`",
|
||||
))?)?;
|
||||
LOGGER.info(format!(
|
||||
"File {path:?} does not exist, creating it with default contents"
|
||||
));
|
||||
let default = T::default();
|
||||
fs::write(path, &serde_json::to_string_pretty(&default)?)?;
|
||||
write_config(path, &default)?;
|
||||
return Ok(default);
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
/target
|
||||
/plugins
|
||||
config.json
|
||||
Generated
+7
@@ -20,6 +20,12 @@ version = "2.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.101"
|
||||
@@ -105,6 +111,7 @@ name = "voxa-server"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"once_cell",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
use voxa_server::{export_plugin, plugin::Plugin};
|
||||
use voxa_server::{export_plugin, logger, plugin::Plugin};
|
||||
|
||||
logger! {
|
||||
const LOGGER "My Plugin"
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MyPlugin;
|
||||
|
||||
impl Plugin for MyPlugin {
|
||||
fn init(&mut self) {
|
||||
println!("MyPlugin initialized!");
|
||||
LOGGER.info("MyPlugin initialized!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-5
@@ -1,9 +1,14 @@
|
||||
use std::path::PathBuf;
|
||||
use voxa_server::plugin::PluginInstance;
|
||||
|
||||
use voxa_server::{ServerConfig, plugin::PluginInstance, vfs};
|
||||
|
||||
fn main() -> voxa_server::Result<()> {
|
||||
let root = PathBuf::from("../");
|
||||
let config = voxa_server::ServerConfig::default();
|
||||
let plugin = test_plugin::MyPlugin::default();
|
||||
config.start_with(vec![PluginInstance::Static(Box::new(plugin))], &root)
|
||||
let root = PathBuf::from("./");
|
||||
let config: ServerConfig = vfs::read_config(&root.join("config.json"))?;
|
||||
let mut server = config.build(&root);
|
||||
server.add_plugin(PluginInstance::Static(Box::new(
|
||||
test_plugin::MyPlugin::default(),
|
||||
)));
|
||||
server.run()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user