diff --git a/.gitignore b/.gitignore index 5c11683..e25729a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /plugins +config.json \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 34c87d7..b7ae25c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1683,6 +1683,8 @@ name = "voxa-server" version = "0.1.0" dependencies = [ "anyhow", + "serde", + "serde_json", "wasmtime", "wasmtime-wasi", ] diff --git a/Cargo.toml b/Cargo.toml index db4c6da..297bc4d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,8 @@ edition = "2024" [dependencies] anyhow = "1.0.99" +serde = { version = "1.0.219", features = ["serde_derive"] } +serde_json = "1.0.143" wasmtime = { version = "36.0.2", optional = true } wasmtime-wasi = { version = "36.0.2", features = ["preview1"], optional = true } diff --git a/src/lib.rs b/src/lib.rs index 51699a7..e7b76b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,11 +10,14 @@ pub use anyhow::Result; use crate::plugin::{Plugin, PluginInstance}; -pub struct ServerConfig {} +#[derive(serde::Serialize, serde::Deserialize)] +pub struct ServerConfig { + port: u16, +} impl Default for ServerConfig { fn default() -> Self { - Self {} + Self { port: 7080 } } } diff --git a/src/loader.rs b/src/loader.rs index 5e66b69..0775ae7 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -1,6 +1,6 @@ use std::path::Path; -use wasmtime::*; +use wasmtime::{Engine, Linker, Module, Store}; use wasmtime_wasi::WasiCtxBuilder; use crate::{PluginInstance, vfs}; diff --git a/src/main.rs b/src/main.rs index b3a7593..b715d6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,10 @@ use std::path::PathBuf; +use voxa_server::{ServerConfig, vfs}; + fn main() -> anyhow::Result<()> { let root = PathBuf::new(); - let config = voxa_server::ServerConfig::default(); + let config: ServerConfig = vfs::read_config(&root.join("config.json"))?; + config.start(&root) } diff --git a/src/vfs.rs b/src/vfs.rs index 35c287f..ebafa88 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -1,5 +1,7 @@ use std::{fs, path::Path}; +use serde::{Deserialize, Serialize}; + pub fn dir(path: &Path) -> crate::Result<()> { if !path.exists() { fs::create_dir_all(path)?; @@ -32,3 +34,47 @@ pub fn read_bytes<'a>(path: &Path, default_content: Vec) -> crate::Result Deserialize<'de>>( + path: &Path, +) -> crate::Result { + 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`", + ))?)?; + let default = T::default(); + fs::write(path, &serde_json::to_string_pretty(&default)?)?; + return Ok(default); + } + + let read = fs::read_to_string(path)?; + Ok(serde_json::from_str::(&read)?) +} + +pub fn write(path: &Path, content: &str) -> crate::Result<()> { + 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, content)?; + Ok(()) +} + +pub fn write_bytes(path: &Path, content: &[u8]) -> crate::Result<()> { + 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, content)?; + Ok(()) +} + +pub fn write_config(path: &Path, content: &T) -> crate::Result<()> { + 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, &serde_json::to_string_pretty(content)?)?; + Ok(()) +} diff --git a/test-plugin/Cargo.lock b/test-plugin/Cargo.lock index 7436edb..7040a56 100644 --- a/test-plugin/Cargo.lock +++ b/test-plugin/Cargo.lock @@ -8,6 +8,85 @@ version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100" +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "memchr" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" + +[[package]] +name = "proc-macro2" +version = "1.0.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.143" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d401abef1d108fbd9cbaebc3e46611f4b1021f714a0597a71f41ee463f5f4a5a" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "test-plugin" version = "0.1.0" @@ -15,9 +94,17 @@ dependencies = [ "voxa-server", ] +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + [[package]] name = "voxa-server" version = "0.1.0" dependencies = [ "anyhow", + "serde", + "serde_json", ]