Skip to content

Commit

Permalink
chore: remove stderr path
Browse files Browse the repository at this point in the history
  • Loading branch information
ereslibre committed Aug 31, 2023
1 parent 0030e7b commit e6202ff
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 64 deletions.
25 changes: 5 additions & 20 deletions crates/server/src/handlers/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use actix_web::{
web::{Bytes, Data},
HttpRequest, HttpResponse,
};
use std::{io::Write, sync::RwLock};
use std::sync::RwLock;
use wws_router::WORKERS;
use wws_worker::io::WasmOutput;

Expand Down Expand Up @@ -79,25 +79,10 @@ pub async fn handle_worker(req: HttpRequest, body: Bytes) -> HttpResponse {
None => None,
};

let stderr_file = app_data
.stderr
.as_ref()
.map(|file| file.try_clone().expect("error setting up stderr"));

let (handler_result, handler_success) =
match worker.run(&req, &body_str, store, vars, &stderr_file) {
Ok(output) => (output, true),
Err(error) => {
if let Some(mut stderr_file) = stderr_file {
stderr_file
.write_all(error.to_string().as_bytes())
.expect("Failed to write error to stderr_file");
} else {
eprintln!("{}", error);
}
(WasmOutput::failed(), false)
}
};
let (handler_result, handler_success) = match worker.run(&req, &body_str, store, vars) {
Ok(output) => (output, true),
Err(_) => (WasmOutput::failed(), false),
};

let mut builder = HttpResponse::build(
StatusCode::from_u16(handler_result.status).unwrap_or(StatusCode::OK),
Expand Down
32 changes: 6 additions & 26 deletions crates/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ use actix_web::{
use handlers::assets::handle_assets;
use handlers::not_found::handle_not_found;
use handlers::worker::handle_worker;
use std::{
fs::{File, OpenOptions},
path::PathBuf,
sync::RwLock,
};
use std::{path::PathBuf, sync::RwLock};
use wws_api_manage::config_manage_api_handlers;
use wws_data_kv::KV;
use wws_panel::config_panel_handlers;
Expand Down Expand Up @@ -54,39 +50,23 @@ pub struct ServeOptions {
pub hostname: String,
pub port: u16,
pub panel: Panel,
pub stderr: Option<PathBuf>,
pub cors_origins: Option<Vec<String>>,
}

#[derive(Default)]
pub struct AppData {
routes: Routes,
root_path: PathBuf,
stderr: Option<File>,
cors_origins: Option<Vec<String>>,
}

impl TryFrom<ServeOptions> for AppData {
type Error = ServeError;

fn try_from(serve_options: ServeOptions) -> Result<Self> {
let stderr = if let Some(stderr) = serve_options.stderr {
Some(
OpenOptions::new()
.append(true)
.open(stderr)
.map_err(|_| ServeError::InitializeServerError)?,
)
} else {
None
};

Ok(AppData {
impl From<ServeOptions> for AppData {
fn from(serve_options: ServeOptions) -> Self {
AppData {
routes: serve_options.base_routes,
root_path: serve_options.root_path.clone(),
stderr,
cors_origins: serve_options.cors_origins.clone(),
})
}
}
}

Expand Down Expand Up @@ -174,7 +154,7 @@ pub async fn serve(serve_options: ServeOptions) -> Result<Server> {
app
})
.bind(format!("{}:{}", hostname, port))
.map_err(|_| errors::ServeError::InitializeServerError)?;
.map_err(|_| ServeError::InitializeServerError)?;

Ok(server.run())
}
19 changes: 2 additions & 17 deletions crates/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use errors::Result;
use features::wasi_nn::WASI_NN_BACKEND_OPENVINO;
use io::{WasmInput, WasmOutput};
use sha256::digest as sha256_digest;
use std::fs::{self, File};
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use std::{collections::HashMap, path::Path};
Expand Down Expand Up @@ -96,25 +96,9 @@ impl Worker {
body: &str,
kv: Option<HashMap<String, String>>,
vars: &HashMap<String, String>,
stderr: &Option<File>,
) -> Result<WasmOutput> {
let input = serde_json::to_string(&WasmInput::new(request, body, kv)).unwrap();

// Prepare the stderr file if present
let stderr_file;

if let Some(file) = stderr {
stderr_file = Some(
file.try_clone()
.map_err(|_| errors::WorkerError::ConfigureRuntimeError)?,
);
} else {
stderr_file = None;
}

// Initialize stdio and configure it
let stdio = Stdio::new(&input, stderr_file);

let mut linker = Linker::new(&self.engine);

http_add_to_linker(&mut linker, |s: &mut WorkerState| &mut s.http)
Expand All @@ -132,6 +116,7 @@ impl Worker {
.map_err(|_| errors::WorkerError::ConfigureRuntimeError)?;

// Configure the stdio
let stdio = Stdio::new(&input, None);
wasi_builder = stdio.configure_wasi_ctx(wasi_builder);

// Mount folders from the configuration
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ async fn main() -> std::io::Result<()> {
hostname: args.hostname.clone(),
port: args.port,
panel: args.enable_panel.into(),
stderr: None,
cors_origins: args.cors,
})
.await
Expand Down

0 comments on commit e6202ff

Please sign in to comment.