use std::path::{Path, PathBuf};
use std::sync::Arc;
use harn_vm::trust_graph::AutonomyTier;
use super::VmConfigurator;
use super::{AuthPolicy, InMemoryReplayCache, LimitRegistry, NoopVmConfigurator, ReplayCache};
pub struct DispatchCoreConfig {
pub script_path: PathBuf,
pub base_dir: PathBuf,
pub service_name: String,
pub autonomy_tier: AutonomyTier,
pub auth_policy: AuthPolicy,
pub replay_cache: Arc<dyn ReplayCache>,
pub vm_configurator: Arc<dyn VmConfigurator>,
pub trusted_host_dispatch: bool,
pub limit_registry: Option<Arc<LimitRegistry>>,
}
impl DispatchCoreConfig {
pub fn for_script(path: impl Into<PathBuf>) -> Self {
let path = path.into();
let script_path = if path.is_absolute() {
path
} else {
std::env::current_dir()
.unwrap_or_else(|_| PathBuf::from("."))
.join(path)
};
let base_dir = script_path.parent().unwrap_or(Path::new(".")).to_path_buf();
let service_name = script_path
.file_stem()
.and_then(|value| value.to_str())
.unwrap_or("harn-serve")
.to_string();
Self {
script_path,
base_dir,
service_name,
autonomy_tier: AutonomyTier::ActAuto,
auth_policy: AuthPolicy::allow_all(),
replay_cache: Arc::new(InMemoryReplayCache::new()),
vm_configurator: Arc::new(NoopVmConfigurator),
trusted_host_dispatch: false,
limit_registry: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn relative_script_paths_are_bound_to_the_launch_directory() {
let relative = PathBuf::from("examples/server.harn");
let config = DispatchCoreConfig::for_script(&relative);
assert!(config.script_path.is_absolute());
assert!(config.script_path.ends_with(&relative));
assert_eq!(config.base_dir, config.script_path.parent().unwrap());
}
}