From 3af48f7028c5ca8f8b569b356b6cd0447f991449 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 1 Mar 2023 22:34:51 +0800 Subject: [PATCH] Hoisted shared state into its own struct --- lib/cli/src/commands/run.rs | 1 - lib/wasi/src/runners/wcgi/handler.rs | 42 +++++++++++++++++++--------- lib/wasi/src/runners/wcgi/runner.rs | 21 ++++++++------ lib/wasi/tests/runners.rs | 2 +- 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index 9ad2c108f..e5b1f4e78 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -227,7 +227,6 @@ impl RunWithPathBuf { fn inner_execute(&self) -> Result<()> { #[cfg(feature = "webc_runner")] { - dbg!(&self.path); if let Ok(pf) = WapmContainer::from_path(self.path.clone()) { return self.run_container(pf, self.command_name.as_deref(), &self.args); } diff --git a/lib/wasi/src/runners/wcgi/handler.rs b/lib/wasi/src/runners/wcgi/handler.rs index 38444b529..970c50f85 100644 --- a/lib/wasi/src/runners/wcgi/handler.rs +++ b/lib/wasi/src/runners/wcgi/handler.rs @@ -1,5 +1,6 @@ use std::{ collections::HashMap, + ops::Deref, path::{Path, PathBuf}, pin::Pin, sync::Arc, @@ -26,21 +27,14 @@ use crate::{ /// The shared object that manages the instantiaion of WASI executables and /// communicating with them via the CGI protocol. -#[derive(Clone, derivative::Derivative)] -#[derivative(Debug)] -pub(crate) struct Handler { - pub(crate) program: Arc, - pub(crate) env: Arc>, - pub(crate) args: Arc<[String]>, - pub(crate) mapped_dirs: Arc<[MappedDirectory]>, - pub(crate) task_manager: Arc, - pub(crate) module: Module, - pub(crate) dialect: CgiDialect, - #[derivative(Debug = "ignore")] - pub(crate) callbacks: Arc, -} +#[derive(Clone, Debug)] +pub(crate) struct Handler(Arc); impl Handler { + pub(crate) fn new(state: SharedState) -> Self { + Handler(Arc::new(state)) + } + pub(crate) async fn handle(&self, req: Request) -> Result, Error> { let (parts, body) = req.into_parts(); @@ -153,6 +147,14 @@ impl Handler { } } +impl Deref for Handler { + type Target = Arc; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + /// Drive the request to completion by streaming the request body to the /// instance and waiting for it to exit. async fn drive_request_to_completion( @@ -216,6 +218,20 @@ async fn consume_stderr( } } +#[derive(Clone, derivative::Derivative)] +#[derivative(Debug)] +pub(crate) struct SharedState { + pub(crate) program: String, + pub(crate) env: HashMap, + pub(crate) args: Vec, + pub(crate) mapped_dirs: Vec, + pub(crate) module: Module, + pub(crate) dialect: CgiDialect, + pub(crate) task_manager: Arc, + #[derivative(Debug = "ignore")] + pub(crate) callbacks: Arc, +} + impl Service> for Handler { type Response = Response; type Error = Error; diff --git a/lib/wasi/src/runners/wcgi/runner.rs b/lib/wasi/src/runners/wcgi/runner.rs index bb2b33ccb..28d32d9c8 100644 --- a/lib/wasi/src/runners/wcgi/runner.rs +++ b/lib/wasi/src/runners/wcgi/runner.rs @@ -12,7 +12,10 @@ use webc::metadata::{ use crate::{ runners::{ - wcgi::{handler::Handler, MappedDirectory}, + wcgi::{ + handler::{Handler, SharedState}, + MappedDirectory, + }, WapmContainer, }, runtime::task_manager::tokio::TokioTaskManager, @@ -20,7 +23,7 @@ use crate::{ }; pub struct WcgiRunner { - program_name: Arc, + program_name: String, config: Config, } @@ -80,7 +83,7 @@ impl WcgiRunner { } impl WcgiRunner { - pub fn new(program_name: impl Into>) -> Self { + pub fn new(program_name: impl Into) -> Self { WcgiRunner { program_name: program_name.into(), config: Config::default(), @@ -118,9 +121,9 @@ impl WcgiRunner { None => CgiDialect::Wcgi, }; - let handler = Handler { - program: Arc::clone(&self.program_name), - env: Arc::new(env), + let shared = SharedState { + program: self.program_name.clone(), + env, args, mapped_dirs: self.config.mapped_dirs.clone().into(), task_manager: self @@ -133,11 +136,11 @@ impl WcgiRunner { callbacks: Arc::clone(&self.config.callbacks), }; - Ok(handler) + Ok(Handler::new(shared)) } } -fn construct_args(wasi: &Wasi, extras: &[String]) -> Arc<[String]> { +fn construct_args(wasi: &Wasi, extras: &[String]) -> Vec { let mut args = Vec::new(); if let Some(main_args) = &wasi.main_args { @@ -146,7 +149,7 @@ fn construct_args(wasi: &Wasi, extras: &[String]) -> Arc<[String]> { args.extend(extras.iter().cloned()); - args.into() + args } fn construct_env( diff --git a/lib/wasi/tests/runners.rs b/lib/wasi/tests/runners.rs index dd7027088..075721fda 100644 --- a/lib/wasi/tests/runners.rs +++ b/lib/wasi/tests/runners.rs @@ -57,7 +57,7 @@ mod wcgi { #[tokio::test] async fn static_server() { - let webc = download_cached("https://wapm.dev/syrusakbary/staticserver").await; + let webc = download_cached("https://wapm.io/Michael-F-Bryan/staticserver").await; let tasks = TokioTaskManager::new(Handle::current()); let container = WapmContainer::from_bytes(webc).unwrap(); let mut runner = WcgiRunner::new("staticserver");