Skip to content

Commit 8ef9d28

Browse files
authored
Merge pull request #750 from lann/simplify-config
Simplify spin-config implementation
2 parents 0e3b3d4 + 763387f commit 8ef9d28

File tree

24 files changed

+381
-612
lines changed

24 files changed

+381
-612
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/config/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ authors = [ "Fermyon Engineering <engineering@fermyon.com>" ]
66

77
[dependencies]
88
anyhow = "1.0"
9+
async-trait = "0.1"
910
serde = { version = "1.0", features = [ "derive" ] }
11+
spin-engine = { path = "../engine" }
12+
spin-manifest = { path = "../manifest" }
1013
thiserror = "1"
1114
tokio = { version = "1", features = [ "rt-multi-thread" ] }
1215

crates/config/src/host_component.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,56 @@
11
use std::sync::Arc;
22

3-
use crate::{Error, Key, Resolver, TreePath};
3+
use spin_engine::host_component::HostComponent;
4+
use spin_manifest::CoreComponent;
5+
use wit_bindgen_wasmtime::async_trait;
6+
7+
use crate::{Error, Key, Resolver};
48

59
mod wit {
610
wit_bindgen_wasmtime::export!({paths: ["../../wit/ephemeral/spin-config.wit"], async: *});
711
}
8-
pub use wit::spin_config::add_to_linker;
9-
use wit_bindgen_wasmtime::async_trait;
1012

11-
/// A component configuration interface implementation.
12-
pub struct ComponentConfig {
13-
component_root: TreePath,
13+
pub struct ConfigHostComponent {
1414
resolver: Arc<Resolver>,
1515
}
1616

17-
impl ComponentConfig {
18-
pub fn new(component_id: impl Into<String>, resolver: Arc<Resolver>) -> crate::Result<Self> {
19-
let component_root = TreePath::new(component_id).or_else(|_| {
20-
// Temporary mitigation for https://github.com/fermyon/spin/issues/337
21-
TreePath::new("invalid.path.issue_337")
22-
})?;
23-
Ok(Self {
24-
component_root,
25-
resolver,
17+
impl ConfigHostComponent {
18+
pub fn new(resolver: Resolver) -> Self {
19+
Self {
20+
resolver: Arc::new(resolver),
21+
}
22+
}
23+
}
24+
25+
impl HostComponent for ConfigHostComponent {
26+
type State = ComponentConfig;
27+
28+
fn add_to_linker<T: Send>(
29+
linker: &mut wit_bindgen_wasmtime::wasmtime::Linker<spin_engine::RuntimeContext<T>>,
30+
state_handle: spin_engine::host_component::HostComponentsStateHandle<Self::State>,
31+
) -> anyhow::Result<()> {
32+
wit::spin_config::add_to_linker(linker, move |ctx| state_handle.get_mut(ctx))
33+
}
34+
35+
fn build_state(&self, component: &CoreComponent) -> anyhow::Result<Self::State> {
36+
Ok(ComponentConfig {
37+
component_id: component.id.clone(),
38+
resolver: self.resolver.clone(),
2639
})
2740
}
2841
}
2942

43+
/// A component configuration interface implementation.
44+
pub struct ComponentConfig {
45+
component_id: String,
46+
resolver: Arc<Resolver>,
47+
}
48+
3049
#[async_trait]
3150
impl wit::spin_config::SpinConfig for ComponentConfig {
3251
async fn get_config(&mut self, key: &str) -> Result<String, wit::spin_config::Error> {
3352
let key = Key::new(key)?;
34-
let path = &self.component_root + key;
35-
// TODO(lann): Make resolve async
36-
tokio::task::block_in_place(|| Ok(self.resolver.resolve(&path)?))
53+
Ok(self.resolver.resolve(&self.component_id, key).await?)
3754
}
3855
}
3956

0 commit comments

Comments
 (0)