|
1 | 1 | use std::sync::Arc;
|
2 | 2 |
|
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}; |
4 | 8 |
|
5 | 9 | mod wit {
|
6 | 10 | wit_bindgen_wasmtime::export!({paths: ["../../wit/ephemeral/spin-config.wit"], async: *});
|
7 | 11 | }
|
8 |
| -pub use wit::spin_config::add_to_linker; |
9 |
| -use wit_bindgen_wasmtime::async_trait; |
10 | 12 |
|
11 |
| -/// A component configuration interface implementation. |
12 |
| -pub struct ComponentConfig { |
13 |
| - component_root: TreePath, |
| 13 | +pub struct ConfigHostComponent { |
14 | 14 | resolver: Arc<Resolver>,
|
15 | 15 | }
|
16 | 16 |
|
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(), |
26 | 39 | })
|
27 | 40 | }
|
28 | 41 | }
|
29 | 42 |
|
| 43 | +/// A component configuration interface implementation. |
| 44 | +pub struct ComponentConfig { |
| 45 | + component_id: String, |
| 46 | + resolver: Arc<Resolver>, |
| 47 | +} |
| 48 | + |
30 | 49 | #[async_trait]
|
31 | 50 | impl wit::spin_config::SpinConfig for ComponentConfig {
|
32 | 51 | async fn get_config(&mut self, key: &str) -> Result<String, wit::spin_config::Error> {
|
33 | 52 | 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?) |
37 | 54 | }
|
38 | 55 | }
|
39 | 56 |
|
|
0 commit comments