Skip to content

Commit

Permalink
Merge pull request #17 from G-Core/fix/env_args
Browse files Browse the repository at this point in the history
fix: parsing envs and headers arg
  • Loading branch information
ruslanti authored Aug 2, 2024
2 parents d0d69df + 9a20b71 commit c203c6e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["crates/*"]
resolver = "2"

[workspace.package]
version = "0.5.1"
version = "0.5.2"
edition = "2021"
publish = false
authors = ["FastEdge Development Team"]
Expand Down
10 changes: 5 additions & 5 deletions crates/http-service/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod wasi_http;
use std::collections::HashMap;
use std::time::{Duration, Instant};

use anyhow::{anyhow, bail, Context, Error, Result};
use anyhow::{anyhow, bail, Context, Result};
use async_trait::async_trait;
use bytesize::ByteSize;
use http::{HeaderMap, HeaderValue, Method, Response};
Expand All @@ -30,7 +30,7 @@ pub trait HttpExecutor {
async fn execute<B>(
&self,
req: hyper::Request<B>,
) -> Result<(Response<BoxBody<Bytes, hyper::Error>>, Duration, ByteSize)>
) -> Result<(Response<BoxBody<Bytes, anyhow::Error>>, Duration, ByteSize)>
where
B: BodyExt + Send,
<B as Body>::Data: Send;
Expand Down Expand Up @@ -62,7 +62,7 @@ where
async fn execute<B>(
&self,
req: hyper::Request<B>,
) -> Result<(Response<BoxBody<Bytes, hyper::Error>>, Duration, ByteSize)>
) -> Result<(Response<BoxBody<Bytes, anyhow::Error>>, Duration, ByteSize)>
where
B: BodyExt + Send,
<B as Body>::Data: Send,
Expand Down Expand Up @@ -93,7 +93,7 @@ where
async fn execute_impl<B>(
&self,
req: hyper::Request<B>,
) -> Result<(Response<BoxBody<Bytes, hyper::Error>>, ByteSize)>
) -> Result<(Response<BoxBody<Bytes, anyhow::Error>>, ByteSize)>
where
B: BodyExt + Send,
<B as Body>::Data: Send,
Expand Down Expand Up @@ -185,7 +185,7 @@ where
.body
.map(|b| Full::from(b).map_err(|never| match never {}).boxed())
.unwrap_or_default();
builder.body(body).map(|r| (r, used)).map_err(Error::msg)
builder.body(body).map(|r| (r, used)).map_err(anyhow::Error::msg)
}

fn get_properties(headers: &HeaderMap<HeaderValue>) -> HashMap<String, String> {
Expand Down
9 changes: 4 additions & 5 deletions crates/http-service/src/executor/wasi_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use http_body_util::{BodyExt, Full};
use hyper::body::{Body, Bytes};
use smol_str::ToSmolStr;
use tracing::error;
use wasmtime_wasi_http::WasiHttpView;
use wasmtime_wasi_http::{ WasiHttpView};

use http_backend::Backend;
use runtime::store::StoreBuilder;
Expand All @@ -35,7 +35,7 @@ where
async fn execute<B>(
&self,
req: hyper::Request<B>,
) -> anyhow::Result<(Response<BoxBody<Bytes, hyper::Error>>, Duration, ByteSize)>
) -> anyhow::Result<(Response<BoxBody<Bytes, anyhow::Error>>, Duration, ByteSize)>
where
B: BodyExt + Send,
<B as Body>::Data: Send,
Expand Down Expand Up @@ -66,7 +66,7 @@ where
async fn execute_impl<B>(
&self,
req: hyper::Request<B>,
) -> anyhow::Result<(Response<BoxBody<Bytes, hyper::Error>>, ByteSize)>
) -> anyhow::Result<(Response<BoxBody<Bytes, anyhow::Error>>, ByteSize)>
where
B: BodyExt,
{
Expand Down Expand Up @@ -168,8 +168,7 @@ where
match receiver.await {
Ok(Ok(resp)) => {
let (parts, body) = resp.into_parts();
let body = body.collect().await.context("response body")?.to_bytes();
let body = Full::new(body).map_err(|never| match never {}).boxed();
let body = body.map_err(anyhow::Error::msg).boxed();
let used = task.await.context("task await")?.context("byte size")?;
Ok((Response::from_parts(parts, body), used))
}
Expand Down
10 changes: 5 additions & 5 deletions crates/http-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ where
&self,
request_id: &str,
mut request: Request<B>,
) -> Result<Response<BoxBody<Bytes, hyper::Error>>>
) -> Result<Response<BoxBody<Bytes, anyhow::Error>>>
where
B: BodyExt + Send,
<B as Body>::Data: Send,
Expand Down Expand Up @@ -407,7 +407,7 @@ fn remote_traceparent(req: &Request<hyper::body::Incoming>) -> String {
}

/// Creates an HTTP 500 response.
fn internal_fastedge_error(msg: &'static str) -> Result<Response<BoxBody<Bytes, hyper::Error>>> {
fn internal_fastedge_error(msg: &'static str) -> Result<Response<BoxBody<Bytes, anyhow::Error>>> {
Ok(Response::builder().status(FASTEDGE_INTERNAL_ERROR).body(
Full::new(Bytes::from(format!("fastedge: {}", msg)))
.map_err(|never| match never {})
Expand All @@ -416,7 +416,7 @@ fn internal_fastedge_error(msg: &'static str) -> Result<Response<BoxBody<Bytes,
}

/// Creates an HTTP 404 response.
fn not_found() -> Result<Response<BoxBody<Bytes, hyper::Error>>> {
fn not_found() -> Result<Response<BoxBody<Bytes, anyhow::Error>>> {
Ok(Response::builder().status(StatusCode::NOT_FOUND).body(
Full::new(Bytes::from("fastedge: Unknown app"))
.map_err(|never| match never {})
Expand All @@ -425,14 +425,14 @@ fn not_found() -> Result<Response<BoxBody<Bytes, hyper::Error>>> {
}

/// Creates an HTTP 429 response.
fn too_many_requests() -> Result<Response<BoxBody<Bytes, hyper::Error>>> {
fn too_many_requests() -> Result<Response<BoxBody<Bytes, anyhow::Error>>> {
Ok(Response::builder()
.status(StatusCode::TOO_MANY_REQUESTS)
.body(Empty::new().map_err(|never| match never {}).boxed())?)
}

/// Creates an HTTP 406 response.
fn not_acceptable() -> Result<Response<BoxBody<Bytes, hyper::Error>>> {
fn not_acceptable() -> Result<Response<BoxBody<Bytes, anyhow::Error>>> {
Ok(Response::builder()
.status(StatusCode::NOT_ACCEPTABLE)
.body(Empty::new().map_err(|never| match never {}).boxed())?)
Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use http_service::executor::{
use http_service::state::HttpState;
use http_service::{ContextHeaders, HttpConfig, HttpService};
use hyper::body::{Body, Bytes};
use hyper::Error;
use hyper_tls::HttpsConnector;
use hyper_util::client::legacy::connect::HttpConnector;
use runtime::app::Status;
Expand Down Expand Up @@ -53,13 +52,13 @@ struct HttpRunArgs {
#[arg(short, long)]
wasm: PathBuf,
/// Environment variable list
#[arg(long, value_parser = parse_key_value::< String, String >)]
#[arg(long, value_parser = parse_key_value::<SmolStr, SmolStr >)]
envs: Option<Vec<(SmolStr, SmolStr)>>,
/// Add header from original request
#[arg(long = "propagate-header", num_args = 0..)]
propagate_headers: Vec<SmolStr>,
/// Execution context headers added to request
#[arg(long, value_parser = parse_key_value::< String, String >)]
#[arg(long, value_parser = parse_key_value::< SmolStr, SmolStr >)]
headers: Option<Vec<(SmolStr, SmolStr)>>,
/// Append sample Geo PoP headers
#[arg(long, default_value = "false")]
Expand Down Expand Up @@ -215,7 +214,7 @@ impl HttpExecutor for CliExecutor {
async fn execute<B>(
&self,
req: Request<B>,
) -> anyhow::Result<(Response<BoxBody<Bytes, Error>>, Duration, ByteSize)>
) -> anyhow::Result<(Response<BoxBody<Bytes, anyhow::Error>>, Duration, ByteSize)>
where
B: BodyExt + Send,
<B as Body>::Data: Send,
Expand Down

0 comments on commit c203c6e

Please sign in to comment.