mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-06 12:48:20 +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<()> {
|
fn inner_execute(&self) -> Result<()> {
|
||||||
#[cfg(feature = "webc_runner")]
|
#[cfg(feature = "webc_runner")]
|
||||||
{
|
{
|
||||||
dbg!(&self.path);
|
|
||||||
if let Ok(pf) = WapmContainer::from_path(self.path.clone()) {
|
if let Ok(pf) = WapmContainer::from_path(self.path.clone()) {
|
||||||
return self.run_container(pf, self.command_name.as_deref(), &self.args);
|
return self.run_container(pf, self.command_name.as_deref(), &self.args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
|
ops::Deref,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
@@ -26,21 +27,14 @@ use crate::{
|
|||||||
|
|
||||||
/// The shared object that manages the instantiaion of WASI executables and
|
/// The shared object that manages the instantiaion of WASI executables and
|
||||||
/// communicating with them via the CGI protocol.
|
/// communicating with them via the CGI protocol.
|
||||||
#[derive(Clone, derivative::Derivative)]
|
#[derive(Clone, Debug)]
|
||||||
#[derivative(Debug)]
|
pub(crate) struct Handler(Arc<SharedState>);
|
||||||
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>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Handler {
|
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> {
|
pub(crate) async fn handle(&self, req: Request<Body>) -> Result<Response<Body>, Error> {
|
||||||
let (parts, body) = req.into_parts();
|
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
|
/// Drive the request to completion by streaming the request body to the
|
||||||
/// instance and waiting for it to exit.
|
/// instance and waiting for it to exit.
|
||||||
async fn drive_request_to_completion(
|
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 {
|
impl Service<Request<Body>> for Handler {
|
||||||
type Response = Response<Body>;
|
type Response = Response<Body>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|||||||
@@ -12,7 +12,10 @@ use webc::metadata::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
runners::{
|
runners::{
|
||||||
wcgi::{handler::Handler, MappedDirectory},
|
wcgi::{
|
||||||
|
handler::{Handler, SharedState},
|
||||||
|
MappedDirectory,
|
||||||
|
},
|
||||||
WapmContainer,
|
WapmContainer,
|
||||||
},
|
},
|
||||||
runtime::task_manager::tokio::TokioTaskManager,
|
runtime::task_manager::tokio::TokioTaskManager,
|
||||||
@@ -20,7 +23,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct WcgiRunner {
|
pub struct WcgiRunner {
|
||||||
program_name: Arc<str>,
|
program_name: String,
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +83,7 @@ impl WcgiRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl WcgiRunner {
|
impl WcgiRunner {
|
||||||
pub fn new(program_name: impl Into<Arc<str>>) -> Self {
|
pub fn new(program_name: impl Into<String>) -> Self {
|
||||||
WcgiRunner {
|
WcgiRunner {
|
||||||
program_name: program_name.into(),
|
program_name: program_name.into(),
|
||||||
config: Config::default(),
|
config: Config::default(),
|
||||||
@@ -118,9 +121,9 @@ impl WcgiRunner {
|
|||||||
None => CgiDialect::Wcgi,
|
None => CgiDialect::Wcgi,
|
||||||
};
|
};
|
||||||
|
|
||||||
let handler = Handler {
|
let shared = SharedState {
|
||||||
program: Arc::clone(&self.program_name),
|
program: self.program_name.clone(),
|
||||||
env: Arc::new(env),
|
env,
|
||||||
args,
|
args,
|
||||||
mapped_dirs: self.config.mapped_dirs.clone().into(),
|
mapped_dirs: self.config.mapped_dirs.clone().into(),
|
||||||
task_manager: self
|
task_manager: self
|
||||||
@@ -133,11 +136,11 @@ impl WcgiRunner {
|
|||||||
callbacks: Arc::clone(&self.config.callbacks),
|
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();
|
let mut args = Vec::new();
|
||||||
|
|
||||||
if let Some(main_args) = &wasi.main_args {
|
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.extend(extras.iter().cloned());
|
||||||
|
|
||||||
args.into()
|
args
|
||||||
}
|
}
|
||||||
|
|
||||||
fn construct_env(
|
fn construct_env(
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ mod wcgi {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn static_server() {
|
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 tasks = TokioTaskManager::new(Handle::current());
|
||||||
let container = WapmContainer::from_bytes(webc).unwrap();
|
let container = WapmContainer::from_bytes(webc).unwrap();
|
||||||
let mut runner = WcgiRunner::new("staticserver");
|
let mut runner = WcgiRunner::new("staticserver");
|
||||||
|
|||||||
Reference in New Issue
Block a user