Share owned host API state between HTTP hooks and native pages

This commit is contained in:
2026-11-07 00:23:57 +00:00
parent 45d099c3e4
commit c91ecbf7a4
14 changed files with 261 additions and 133 deletions
+17 -10
View File
@@ -164,7 +164,7 @@ impl Runtime {
}
fn with_compiler(world: World, compiler: Arc<cache::Compiler>) -> anyhow::Result<Arc<Self>> {
let cred = Arc::new(crate::cred::Cred::open(&world.host_dir)?);
Self::with_hooks(world, compiler, cred, default_script(), http::whoami)
Self::with_hooks(world, compiler, cred, default_script(), None)
}
/// Share credentials and the host API router with the host's other subsystems.
pub fn new_with_hooks(
@@ -173,21 +173,24 @@ impl Runtime {
script: Arc<dyn crate::cred::script::Script>,
router: Option<Router>,
) -> anyhow::Result<Arc<Self>> {
Self::with_hooks(
world,
cache::shared()?,
cred,
script,
router.unwrap_or(http::whoami),
)
Self::with_hooks(world, cache::shared()?, cred, script, router)
}
fn with_hooks(
world: World,
compiler: Arc<cache::Compiler>,
cred: Arc<crate::cred::Cred>,
script: Arc<dyn crate::cred::script::Script>,
router: http::Router,
router: Option<http::Router>,
) -> anyhow::Result<Arc<Self>> {
let router = match router {
Some(api) => api,
None => Arc::new(crate::api::Api::open(
cred.clone(),
&world.host_dir,
"localhost",
"http://localhost:8081",
)?),
};
let network = Arc::new(net::NetPolicy::open(&world.host_dir)?);
let engine = compiler.engine.clone();
let alive = Arc::new(AtomicBool::new(true));
@@ -215,6 +218,10 @@ impl Runtime {
cache::precompile(runtime.compiler.clone(), runtime.world.root.clone());
Ok(runtime)
}
/// Register native listener ports through the shared network policy.
pub fn network_policy(&self) -> &net::NetPolicy {
&self.network
}
/// Number of distinct program contents compiled by this runtime.
pub fn compiled_programs(&self) -> usize {
self.cache.lock().unwrap().len()
@@ -428,7 +435,7 @@ impl Runtime {
cred: self.cred.clone(),
script: self.script.clone(),
policy: self.network.clone(),
router: self.router,
router: self.router.clone(),
},
},
);