use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use super::schema::Role;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AllowedClient {
pub id: String,
pub pubkey: String,
pub hmac_secret: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
pub role: Role,
pub listen_port: u16,
pub interface: String,
pub ip: String,
#[serde(default = "default_data_dir")]
pub data_dir: String,
pub public_key: String,
#[serde(default)]
pub allowed_clients: Vec<AllowedClient>,
}
fn default_data_dir() -> String {
"/var/lib/porta".to_string()
}
impl ServerConfig {
pub fn save(&self) -> Result<()> {
let config_dir = PathBuf::from("/etc/porta");
fs::create_dir_all(&config_dir)?;
let config_path = config_dir.join("server.toml");
let content = toml::to_string_pretty(self)?;
fs::write(&config_path, content)?;
Ok(())
}
pub fn data_path(&self) -> PathBuf {
PathBuf::from(&self.data_dir)
}
}