mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-06 20:58:28 +00:00
Hoisted shared state into its own struct
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<str>,
|
||||
pub(crate) env: Arc<HashMap<String, String>>,
|
||||
pub(crate) args: Arc<[String]>,
|
||||
pub(crate) mapped_dirs: Arc<[MappedDirectory]>,
|
||||
pub(crate) task_manager: Arc<dyn VirtualTaskManager>,
|
||||
pub(crate) module: Module,
|
||||
pub(crate) dialect: CgiDialect,
|
||||
#[derivative(Debug = "ignore")]
|
||||
pub(crate) callbacks: Arc<dyn Callbacks>,
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Handler(Arc<SharedState>);
|
||||
|
||||
impl Handler {
|
||||
pub(crate) fn new(state: SharedState) -> Self {
|
||||
Handler(Arc::new(state))
|
||||
}
|
||||
|
||||
pub(crate) async fn handle(&self, req: Request<Body>) -> Result<Response<Body>, Error> {
|
||||
let (parts, body) = req.into_parts();
|
||||
|
||||
@@ -153,6 +147,14 @@ impl Handler {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Handler {
|
||||
type Target = Arc<SharedState>;
|
||||
|
||||
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<String, String>,
|
||||
pub(crate) args: Vec<String>,
|
||||
pub(crate) mapped_dirs: Vec<MappedDirectory>,
|
||||
pub(crate) module: Module,
|
||||
pub(crate) dialect: CgiDialect,
|
||||
pub(crate) task_manager: Arc<dyn VirtualTaskManager>,
|
||||
#[derivative(Debug = "ignore")]
|
||||
pub(crate) callbacks: Arc<dyn Callbacks>,
|
||||
}
|
||||
|
||||
impl Service<Request<Body>> for Handler {
|
||||
type Response = Response<Body>;
|
||||
type Error = Error;
|
||||
|
||||
@@ -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<str>,
|
||||
program_name: String,
|
||||
config: Config,
|
||||
}
|
||||
|
||||
@@ -80,7 +83,7 @@ impl WcgiRunner {
|
||||
}
|
||||
|
||||
impl WcgiRunner {
|
||||
pub fn new(program_name: impl Into<Arc<str>>) -> Self {
|
||||
pub fn new(program_name: impl Into<String>) -> 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<String> {
|
||||
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(
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user