Skip to content

Commit

Permalink
Slim down surface of loader system
Browse files Browse the repository at this point in the history
  • Loading branch information
itowlson committed Feb 2, 2022
1 parent 4a83813 commit 2d42e58
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 88 deletions.
13 changes: 0 additions & 13 deletions src/handler_compiler.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/handler_config.rs

This file was deleted.

40 changes: 40 additions & 0 deletions src/handler_loader/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::path::{PathBuf};

use anyhow::Context;

use crate::wasm_module::WasmModuleSource;

use super::{loader::{LoadedHandlerConfiguration, LoadedHandlerConfigurationEntry}, WasmHandlerConfiguration, WasmHandlerConfigurationEntry};

pub struct WasmCompilationSettings {
pub cache_config_path: PathBuf,
}

pub fn compile(uncompiled_handlers: LoadedHandlerConfiguration, compilation_settings: WasmCompilationSettings) -> anyhow::Result<WasmHandlerConfiguration> {
uncompiled_handlers.compile_modules(|module_bytes|
crate::wasm_module::WasmModuleSource::from_module_bytes(module_bytes, &compilation_settings.cache_config_path)
)
}

impl LoadedHandlerConfiguration {
pub fn compile_modules(self, compile: impl Fn(std::sync::Arc<Vec<u8>>) -> anyhow::Result<WasmModuleSource>) -> anyhow::Result<WasmHandlerConfiguration> {
let result: anyhow::Result<Vec<WasmHandlerConfigurationEntry>> =
self
.entries
.into_iter()
.map(|e| e.compile_module(|m| compile(m)))
.collect();
Ok(WasmHandlerConfiguration { entries: result? })
}
}

impl LoadedHandlerConfigurationEntry {
pub fn compile_module(self, compile: impl Fn(std::sync::Arc<Vec<u8>>) -> anyhow::Result<WasmModuleSource>) -> anyhow::Result<WasmHandlerConfigurationEntry> {
let compiled_module = compile(self.module)
.with_context(|| format!("Error compiling Wasm module {}", &self.info.name))?;
Ok(WasmHandlerConfigurationEntry {
info: self.info,
module: compiled_module,
})
}
}
12 changes: 9 additions & 3 deletions src/handler_loader/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ use serde::Deserialize;

use crate::{wagi_config::WagiConfiguration, bindle_util::{InvoiceUnderstander, WagiHandlerInfo}, module_loader::Loaded};

use super::{emplacer::{PreHandlerConfiguration, Emplacer}, LoadedHandlerConfigurationImpl, LoadedHandlerConfigurationEntryImpl, HandlerInfo};
use super::{emplacer::{PreHandlerConfiguration, Emplacer}, HandlerInfo};

pub type LoadedHandlerConfiguration = LoadedHandlerConfigurationImpl<std::sync::Arc<Vec<u8>>>;
pub type LoadedHandlerConfigurationEntry = LoadedHandlerConfigurationEntryImpl<std::sync::Arc<Vec<u8>>>;
pub struct LoadedHandlerConfiguration {
pub entries: Vec<LoadedHandlerConfigurationEntry>,
}

pub struct LoadedHandlerConfigurationEntry {
pub info: HandlerInfo,
pub module: std::sync::Arc<Vec<u8>>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct ModuleMapConfiguration {
Expand Down
57 changes: 9 additions & 48 deletions src/handler_loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ use std::collections::HashMap;

use anyhow::Context;

use crate::{wagi_config::{WagiConfiguration}, handler_compiler::{WasmCompilationSettings, compile_all}};
use crate::{wagi_config::WagiConfiguration, wasm_module::WasmModuleSource};

mod emplacer;
mod loader;
mod compiler;

pub use loader::LoadedHandlerConfiguration;
// pub use loader::LoadedHandlerConfiguration;
pub use loader::ModuleMapConfigurationEntry;
pub use compiler::WasmCompilationSettings;

pub async fn load_handlers(configuration: &WagiConfiguration) -> anyhow::Result<WasmHandlerConfiguration> {
let emplaced_handlers = emplacer::emplace(&configuration /* configuration.handlers, configuration.placement_settings() */).await
.with_context(|| "Failed to copy modules and assets to local cache")?;
let loaded_handlers = loader::load(emplaced_handlers, &configuration /* .loader_settings() */).await
.with_context(|| "Failed to load one or more Wasm modules from source")?;
let handlers = compile(loaded_handlers, configuration.wasm_compilation_settings())
let handlers = compiler::compile(loaded_handlers, configuration.wasm_compilation_settings())
.with_context(|| "Failed to compile one or more Wasm modules")?;
Ok(handlers)
}

pub type WasmHandlerConfiguration = LoadedHandlerConfigurationImpl<crate::wasm_module::WasmModuleSource>;
pub type WasmHandlerConfigurationEntry = LoadedHandlerConfigurationEntryImpl<crate::wasm_module::WasmModuleSource>;

pub struct HandlerInfo {
pub name: String,
pub route: String,
Expand All @@ -32,49 +31,11 @@ pub struct HandlerInfo {
pub volume_mounts: HashMap<String, String>,
}

pub struct LoadedHandlerConfigurationImpl<M> {
pub entries: Vec<LoadedHandlerConfigurationEntryImpl<M>>,
pub struct WasmHandlerConfiguration {
pub entries: Vec<WasmHandlerConfigurationEntry>,
}

pub struct LoadedHandlerConfigurationEntryImpl<M> {
pub struct WasmHandlerConfigurationEntry {
pub info: HandlerInfo,
pub module: M,
}

impl<M> LoadedHandlerConfigurationImpl<M> {
pub fn convert_modules<O>(self, compile: impl Fn(M) -> anyhow::Result<O>) -> anyhow::Result<LoadedHandlerConfigurationImpl<O>> {
let result: anyhow::Result<Vec<LoadedHandlerConfigurationEntryImpl<O>>> =
self
.entries
.into_iter()
.map(|e| e.convert_module(|m| compile(m)))
.collect();
Ok(LoadedHandlerConfigurationImpl { entries: result? })
}
}

impl<M> LoadedHandlerConfigurationEntryImpl<M> {
pub fn convert_module<O>(self, compile: impl Fn(M) -> anyhow::Result<O>) -> anyhow::Result<LoadedHandlerConfigurationEntryImpl<O>> {
let compiled_module = compile(self.module)
.with_context(|| format!("Error compiling Wasm module {}", &self.info.name))?;
Ok(LoadedHandlerConfigurationEntryImpl {
info: self.info,
module: compiled_module,
})
}
}

pub fn compile(loaded_handlers: LoadedHandlerConfiguration, settings: WasmCompilationSettings) -> anyhow::Result<WasmHandlerConfiguration> {
compile_all(loaded_handlers, settings)
pub module: WasmModuleSource,
}

// // TODO: we might need to do some renaming here to reflect that the source
// // may include non-handler roles in future
// async fn read_handler_configuration(pre_handler_config: PreHandlerConfiguration) -> anyhow::Result<HandlerConfiguration> {
// match pre_handler_config {
// PreHandlerConfiguration::ModuleMapFile(path) =>
// read_module_map_configuration(&path).await.map(HandlerConfiguration::ModuleMapFile),
// PreHandlerConfiguration::Bindle(emplacer, invoice) =>
// Ok(HandlerConfiguration::Bindle(emplacer, invoice)),
// }
// }
24 changes: 2 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pub(crate) mod bindle_util;
pub mod dispatcher;
pub(crate) mod dynamic_route;
pub(crate) mod handler_compiler;
pub(crate) mod handler_config;
pub mod handler_loader;
pub(crate) mod handlers;
mod http_util;
Expand Down Expand Up @@ -93,20 +91,11 @@ mod test {
"-b", bindle_id,
"--bindle-path", &test_standalone_bindle_data_dir().display().to_string(),
]);

let configuration = wagi_app::parse_configuration_from(matches)
.expect("Fake command line was not valid");

// let emplacer = crate::emplacer::Emplacer::new(&configuration).await
// .expect("Failed to create emplacer");
// let pre_handler_config = emplacer.emplace_all().await
// .expect("Failed to emplace bindle data");
// let uncompiled_handlers = configuration.load_handler_configuration(pre_handler_config).await
// .expect("Failed to load handlers");
// let handlers = compile_all(uncompiled_handlers, configuration.wasm_compilation_settings())
// .expect("Failed to compile Wasm modules");
let handlers = crate::handler_loader::load_handlers(&configuration).await
.expect("Failed to load handlers");

crate::dispatcher::RoutingTable::build(&handlers, configuration.request_global_context())
.expect("Failed to build routing table")
}
Expand All @@ -131,20 +120,11 @@ mod test {
"wagi",
"-c", &modules_toml_path.display().to_string(),
]);

let configuration = wagi_app::parse_configuration_from(matches)
.expect("Fake command line was not valid");

// let emplacer = crate::emplacer::Emplacer::new(&configuration).await
// .expect("Failed to create emplacer");
// let pre_handler_config = emplacer.emplace_all().await
// .expect("Failed to emplace bindle data");
// let uncompiled_handlers = configuration.load_handler_configuration(pre_handler_config).await
// .expect("Failed to load handlers");
// let handlers = compile_all(uncompiled_handlers, configuration.wasm_compilation_settings())
// .expect("Failed to compile Wasm modules");
let handlers = crate::handler_loader::load_handlers(&configuration).await
.expect("Failed to load handlers");

crate::dispatcher::RoutingTable::build(&handlers, configuration.request_global_context())
.expect("Failed to build routing table")
}
Expand Down
2 changes: 1 addition & 1 deletion src/wagi_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, net::SocketAddr, path::{PathBuf}};

use crate::{request::RequestGlobalContext, handler_compiler::WasmCompilationSettings};
use crate::{request::RequestGlobalContext, handler_loader::WasmCompilationSettings};

#[derive(Clone, Debug)]
pub struct WagiConfiguration {
Expand Down

0 comments on commit 2d42e58

Please sign in to comment.