Improved config stuff
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
/plugins
|
/plugins
|
||||||
|
config.json
|
||||||
Generated
+2
@@ -1683,6 +1683,8 @@ name = "voxa-server"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"wasmtime",
|
"wasmtime",
|
||||||
"wasmtime-wasi",
|
"wasmtime-wasi",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.99"
|
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 = { version = "36.0.2", optional = true }
|
||||||
wasmtime-wasi = { version = "36.0.2", features = ["preview1"], optional = true }
|
wasmtime-wasi = { version = "36.0.2", features = ["preview1"], optional = true }
|
||||||
|
|
||||||
|
|||||||
+5
-2
@@ -10,11 +10,14 @@ pub use anyhow::Result;
|
|||||||
|
|
||||||
use crate::plugin::{Plugin, PluginInstance};
|
use crate::plugin::{Plugin, PluginInstance};
|
||||||
|
|
||||||
pub struct ServerConfig {}
|
#[derive(serde::Serialize, serde::Deserialize)]
|
||||||
|
pub struct ServerConfig {
|
||||||
|
port: u16,
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for ServerConfig {
|
impl Default for ServerConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {}
|
Self { port: 7080 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use wasmtime::*;
|
use wasmtime::{Engine, Linker, Module, Store};
|
||||||
use wasmtime_wasi::WasiCtxBuilder;
|
use wasmtime_wasi::WasiCtxBuilder;
|
||||||
|
|
||||||
use crate::{PluginInstance, vfs};
|
use crate::{PluginInstance, vfs};
|
||||||
|
|||||||
+4
-1
@@ -1,7 +1,10 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use voxa_server::{ServerConfig, vfs};
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let root = PathBuf::new();
|
let root = PathBuf::new();
|
||||||
let config = voxa_server::ServerConfig::default();
|
let config: ServerConfig = vfs::read_config(&root.join("config.json"))?;
|
||||||
|
|
||||||
config.start(&root)
|
config.start(&root)
|
||||||
}
|
}
|
||||||
|
|||||||
+46
@@ -1,5 +1,7 @@
|
|||||||
use std::{fs, path::Path};
|
use std::{fs, path::Path};
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub fn dir(path: &Path) -> crate::Result<()> {
|
pub fn dir(path: &Path) -> crate::Result<()> {
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
fs::create_dir_all(path)?;
|
fs::create_dir_all(path)?;
|
||||||
@@ -32,3 +34,47 @@ pub fn read_bytes<'a>(path: &Path, default_content: Vec<u8>) -> crate::Result<Ve
|
|||||||
|
|
||||||
Ok(fs::read(path)?)
|
Ok(fs::read(path)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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`",
|
||||||
|
))?)?;
|
||||||
|
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::<T>(&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<T: Serialize>(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(())
|
||||||
|
}
|
||||||
|
|||||||
Generated
+87
@@ -8,6 +8,85 @@ version = "1.0.99"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100"
|
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]]
|
[[package]]
|
||||||
name = "test-plugin"
|
name = "test-plugin"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@@ -15,9 +94,17 @@ dependencies = [
|
|||||||
"voxa-server",
|
"voxa-server",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "voxa-server"
|
name = "voxa-server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user