Improved config stuff

This commit is contained in:
2025-08-29 19:41:46 +02:00
parent 494ecd6204
commit 6797007526
8 changed files with 148 additions and 4 deletions
+5 -2
View File
@@ -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 }
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
use std::path::Path;
use wasmtime::*;
use wasmtime::{Engine, Linker, Module, Store};
use wasmtime_wasi::WasiCtxBuilder;
use crate::{PluginInstance, vfs};
+4 -1
View File
@@ -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)
}
+46
View File
@@ -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<u8>) -> crate::Result<Ve
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(())
}