|
| 1 | +mod host_component; |
| 2 | +pub mod locked; |
| 3 | +pub mod values; |
| 4 | + |
| 5 | +pub use async_trait::async_trait; |
| 6 | +pub use host_component::DynamicHostComponent; |
| 7 | + |
| 8 | +use host_component::DynamicHostComponents; |
| 9 | +use locked::{ContentPath, LockedApp, LockedComponent, LockedComponentSource, LockedTrigger}; |
| 10 | +use ouroboros::self_referencing; |
| 11 | +use serde::Deserialize; |
| 12 | +use spin_core::{wasmtime, Engine, EngineBuilder, StoreBuilder}; |
| 13 | + |
| 14 | +// TODO(lann): Should this migrate to spin-loader? |
| 15 | +#[async_trait] |
| 16 | +pub trait Loader { |
| 17 | + async fn load_app(&self, uri: &str) -> anyhow::Result<LockedApp>; |
| 18 | + |
| 19 | + async fn load_module( |
| 20 | + &self, |
| 21 | + engine: &wasmtime::Engine, |
| 22 | + source: &LockedComponentSource, |
| 23 | + ) -> anyhow::Result<spin_core::Module>; |
| 24 | + |
| 25 | + async fn mount_files( |
| 26 | + &self, |
| 27 | + store_builder: &mut StoreBuilder, |
| 28 | + component: &AppComponent, |
| 29 | + ) -> anyhow::Result<()>; |
| 30 | +} |
| 31 | + |
| 32 | +pub struct AppLoader { |
| 33 | + inner: Box<dyn Loader + Send + Sync>, |
| 34 | + dynamic_host_components: DynamicHostComponents, |
| 35 | +} |
| 36 | + |
| 37 | +impl AppLoader { |
| 38 | + pub fn new(loader: impl Loader + Send + Sync + 'static) -> Self { |
| 39 | + Self { |
| 40 | + inner: Box::new(loader), |
| 41 | + dynamic_host_components: Default::default(), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + pub fn add_dynamic_host_component<T: Send + Sync, DHC: DynamicHostComponent>( |
| 46 | + &mut self, |
| 47 | + engine_builder: &mut EngineBuilder<T>, |
| 48 | + host_component: DHC, |
| 49 | + ) -> anyhow::Result<()> { |
| 50 | + self.dynamic_host_components |
| 51 | + .add_dynamic_host_component(engine_builder, host_component) |
| 52 | + } |
| 53 | + |
| 54 | + pub async fn load_app(&self, uri: String) -> Result<App> { |
| 55 | + let locked = self |
| 56 | + .inner |
| 57 | + .load_app(&uri) |
| 58 | + .await |
| 59 | + .map_err(Error::LoaderError)?; |
| 60 | + Ok(App { |
| 61 | + loader: self, |
| 62 | + uri, |
| 63 | + locked, |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + pub async fn load_owned_app(self, uri: String) -> Result<OwnedApp> { |
| 68 | + OwnedApp::try_new_async(self, |loader| Box::pin(loader.load_app(uri))).await |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl std::fmt::Debug for AppLoader { |
| 73 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 74 | + f.debug_struct("AppLoader").finish() |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[self_referencing] |
| 79 | +#[derive(Debug)] |
| 80 | +pub struct OwnedApp { |
| 81 | + loader: AppLoader, |
| 82 | + #[borrows(loader)] |
| 83 | + #[covariant] |
| 84 | + app: App<'this>, |
| 85 | +} |
| 86 | + |
| 87 | +impl std::ops::Deref for OwnedApp { |
| 88 | + type Target = App<'static>; |
| 89 | + |
| 90 | + fn deref(&self) -> &Self::Target { |
| 91 | + unsafe { |
| 92 | + // We know that App's lifetime param is for AppLoader, which is owned by self here. |
| 93 | + std::mem::transmute::<&App, &App<'static>>(self.borrow_app()) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[derive(Debug)] |
| 99 | +pub struct App<'a> { |
| 100 | + loader: &'a AppLoader, |
| 101 | + uri: String, |
| 102 | + locked: LockedApp, |
| 103 | +} |
| 104 | + |
| 105 | +impl<'a> App<'a> { |
| 106 | + pub fn uri(&self) -> &str { |
| 107 | + &self.uri |
| 108 | + } |
| 109 | + |
| 110 | + pub fn get_metadata<'this, T: Deserialize<'this>>(&'this self, key: &str) -> Option<Result<T>> { |
| 111 | + self.locked |
| 112 | + .metadata |
| 113 | + .get(key) |
| 114 | + .map(|value| Ok(T::deserialize(value)?)) |
| 115 | + } |
| 116 | + |
| 117 | + pub fn components(&self) -> impl Iterator<Item = AppComponent> { |
| 118 | + self.locked |
| 119 | + .components |
| 120 | + .iter() |
| 121 | + .map(|locked| AppComponent { app: self, locked }) |
| 122 | + } |
| 123 | + |
| 124 | + pub fn get_component(&self, component_id: &str) -> Option<AppComponent> { |
| 125 | + self.components() |
| 126 | + .find(|component| component.locked.id == component_id) |
| 127 | + } |
| 128 | + |
| 129 | + pub fn triggers(&self) -> impl Iterator<Item = AppTrigger> { |
| 130 | + self.locked |
| 131 | + .triggers |
| 132 | + .iter() |
| 133 | + .map(|locked| AppTrigger { app: self, locked }) |
| 134 | + } |
| 135 | + |
| 136 | + pub fn triggers_with_type(&'a self, trigger_type: &'a str) -> impl Iterator<Item = AppTrigger> { |
| 137 | + self.triggers() |
| 138 | + .filter(move |trigger| trigger.locked.trigger_type == trigger_type) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +pub struct AppComponent<'a> { |
| 143 | + pub app: &'a App<'a>, |
| 144 | + locked: &'a LockedComponent, |
| 145 | +} |
| 146 | + |
| 147 | +impl<'a> AppComponent<'a> { |
| 148 | + pub fn id(&self) -> &str { |
| 149 | + &self.locked.id |
| 150 | + } |
| 151 | + |
| 152 | + pub fn source(&self) -> &LockedComponentSource { |
| 153 | + &self.locked.source |
| 154 | + } |
| 155 | + |
| 156 | + pub fn files(&self) -> std::slice::Iter<ContentPath> { |
| 157 | + self.locked.files.iter() |
| 158 | + } |
| 159 | + |
| 160 | + pub fn get_metadata<T: Deserialize<'a>>(&self, key: &str) -> Option<Result<T>> { |
| 161 | + self.locked |
| 162 | + .metadata |
| 163 | + .get(key) |
| 164 | + .map(|value| Ok(T::deserialize(value)?)) |
| 165 | + } |
| 166 | + |
| 167 | + pub async fn load_module<T: Send + Sync>( |
| 168 | + &self, |
| 169 | + engine: &Engine<T>, |
| 170 | + ) -> Result<spin_core::Module> { |
| 171 | + self.app |
| 172 | + .loader |
| 173 | + .inner |
| 174 | + .load_module(engine.as_ref(), &self.locked.source) |
| 175 | + .await |
| 176 | + .map_err(Error::LoaderError) |
| 177 | + } |
| 178 | + |
| 179 | + pub async fn apply_store_config(&self, builder: &mut StoreBuilder) -> Result<()> { |
| 180 | + builder.env(&self.locked.env).map_err(Error::CoreError)?; |
| 181 | + |
| 182 | + let loader = self.app.loader; |
| 183 | + loader |
| 184 | + .inner |
| 185 | + .mount_files(builder, self) |
| 186 | + .await |
| 187 | + .map_err(Error::LoaderError)?; |
| 188 | + |
| 189 | + loader |
| 190 | + .dynamic_host_components |
| 191 | + .update_data(builder.host_components_data(), self) |
| 192 | + .map_err(Error::HostComponentError)?; |
| 193 | + |
| 194 | + Ok(()) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +pub struct AppTrigger<'a> { |
| 199 | + pub app: &'a App<'a>, |
| 200 | + locked: &'a LockedTrigger, |
| 201 | +} |
| 202 | + |
| 203 | +impl<'a> AppTrigger<'a> { |
| 204 | + pub fn id(&self) -> &str { |
| 205 | + &self.locked.id |
| 206 | + } |
| 207 | + |
| 208 | + pub fn trigger_type(&self) -> &str { |
| 209 | + &self.locked.trigger_type |
| 210 | + } |
| 211 | + |
| 212 | + pub fn component(&self) -> Result<AppComponent<'a>> { |
| 213 | + let component_id = self.locked.trigger_config.get("component").ok_or_else(|| { |
| 214 | + Error::ManifestError(format!( |
| 215 | + "trigger {:?} missing 'component' config field", |
| 216 | + self.locked.id |
| 217 | + )) |
| 218 | + })?; |
| 219 | + let component_id = component_id.as_str().ok_or_else(|| { |
| 220 | + Error::ManifestError(format!( |
| 221 | + "trigger {:?} 'component' field has unexpected value {:?}", |
| 222 | + self.locked.id, component_id |
| 223 | + )) |
| 224 | + })?; |
| 225 | + self.app.get_component(component_id).ok_or_else(|| { |
| 226 | + Error::ManifestError(format!( |
| 227 | + "missing component {:?} configured for trigger {:?}", |
| 228 | + component_id, self.locked.id |
| 229 | + )) |
| 230 | + }) |
| 231 | + } |
| 232 | + |
| 233 | + pub fn typed_config<Config: Deserialize<'a>>(&self) -> Result<Config> { |
| 234 | + Ok(Config::deserialize(&self.locked.trigger_config)?) |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +pub type Result<T> = std::result::Result<T, Error>; |
| 239 | + |
| 240 | +#[derive(Debug, thiserror::Error)] |
| 241 | +pub enum Error { |
| 242 | + #[error("spin core error: {0}")] |
| 243 | + CoreError(anyhow::Error), |
| 244 | + #[error("host component error: {0}")] |
| 245 | + HostComponentError(anyhow::Error), |
| 246 | + #[error("loader error: {0}")] |
| 247 | + LoaderError(anyhow::Error), |
| 248 | + #[error("manifest error: {0}")] |
| 249 | + ManifestError(String), |
| 250 | + #[error("json error: {0}")] |
| 251 | + JsonError(#[from] serde_json::Error), |
| 252 | +} |
0 commit comments