Skip to content

Commit

Permalink
Merge pull request #38 from polywrap/develop
Browse files Browse the repository at this point in the history
Release 0.2.3
  • Loading branch information
nerfZael authored Sep 4, 2023
2 parents 5ec3647 + 900c33e commit c3f1e9a
Show file tree
Hide file tree
Showing 45 changed files with 6,004 additions and 93 deletions.
42 changes: 27 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
"packages/wrap-utils-wasm",
"packages/easy-error-string",
"packages/wraps/script-wrap-manager",
"packages/wraps/multipart",
"packages/plugins/http-server",
"packages/plugins/key-value-store",
"examples/simple-pwr-app",
Expand All @@ -31,6 +32,7 @@ default-members = [
"packages/plugins/http-server",
"packages/plugins/key-value-store",
"packages/wraps/script-wrap-manager",
"packages/wraps/multipart",
"examples/simple-pwr-app",
"examples/http-server-app",
"examples/simple-storage-app",
Expand Down
2 changes: 2 additions & 0 deletions examples/http-server-app/polywrap.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import { Module, Response, KeyValuePair, Request } into HttpServer from "wrap://http/http.wrappers.dev/u/test/http-server"
#import * into Multipart from "wrap://http/http.wrappers.dev/u/test/multipart"

type Module {
main(args: [String!]!): Int32!
Expand All @@ -7,4 +8,5 @@ type Module {
routeWithParam(request: HttpServer_Request!): HttpServer_Response!
routeWithQuery(request: HttpServer_Request!): HttpServer_Response!
routePost(request: HttpServer_Request!): HttpServer_Response!
routeUpload(request: HttpServer_Request!): HttpServer_Response!
}
59 changes: 35 additions & 24 deletions examples/http-server-app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod wrap;

use polywrap_wasm_rs::{wrap_debug_log, JSON::json};
use serde::Serialize;
use serde_bytes::ByteBuf;
Expand Down Expand Up @@ -55,6 +56,14 @@ impl ModuleTrait for Module {
method: "routePost".to_string()
}
},
HttpServerRoute {
path: "/upload".to_string(),
http_method: HttpServerHttpMethod::POST,
handler: HttpServerWrapperCallback {
uri: "http/http.wrappers.dev/u/test/http-server-pwr-app".to_string(),
method: "routeUpload".to_string()
}
},
],
on_start: Some(
HttpServerWrapperCallback {
Expand Down Expand Up @@ -127,32 +136,34 @@ impl ModuleTrait for Module {
body: args.request.body.map(|x| ByteBuf::from(x.to_vec())),
})
}
}

fn to_json_response<T: Serialize>(data: T) -> HttpServerResponse {
HttpServerResponse {
status_code: 200,
headers: Some(vec![HttpServerKeyValuePair {
key: "Content-Type".to_string(),
value: "application/json".to_string(),
}]),
body: Some(
ByteBuf::from(json!(data)
.to_string()
.as_bytes()
.to_vec())
),
}
}
fn route_upload(args: ArgsRouteUpload) -> Result<HttpServerResponse, String> {
log(format!("Route upload"));

let files = MultipartModule::get_files(&ArgsGetFiles {
headers: args.request.headers.iter().map(|x| MultipartKeyValuePair {
key: x.key.clone(),
value: x.value.clone()
}).collect::<Vec<MultipartKeyValuePair>>(),
body: args.request.body.unwrap()
}).unwrap();

fn to_error_response(message: String) -> HttpServerResponse {
HttpServerResponse {
status_code: 500,
headers: Some(vec![HttpServerKeyValuePair {
key: "Content-Type".to_string(),
value: "text/html".to_string(),
}]),
body: Some(ByteBuf::from(message.as_bytes().to_vec())),
log(format!("Found the following files: {}", files.iter().map(|x| x.name.clone()).collect::<Vec<String>>().join(", ")));

Ok(HttpServerResponse {
status_code: 200,
headers: Some(vec![
HttpServerKeyValuePair {
key: "Content-Type".to_string(),
value: "text/html".to_string(),
},
HttpServerKeyValuePair {
key: "Content-Disposition".to_string(),
value: format!("attachment; filename=\"{}\"", files[0].name),
}
]),
body: Some(files[0].content.clone()),
})
}
}

Expand Down
6 changes: 5 additions & 1 deletion examples/http-server-app/src/wrap/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::{
route_home_wrapped,
route_with_param_wrapped,
route_with_query_wrapped,
route_post_wrapped
route_post_wrapped,
route_upload_wrapped
};
use polywrap_wasm_rs::{
abort,
Expand Down Expand Up @@ -39,6 +40,9 @@ pub extern "C" fn _wrap_invoke(method_size: u32, args_size: u32, env_size: u32)
"routePost" => {
result = route_post_wrapped(args.args.as_slice(), env_size);
}
"routeUpload" => {
result = route_upload_wrapped(args.args.as_slice(), env_size);
}
_ => {
invoke::wrap_invoke_error(format!("Could not find invoke function {}", args.method));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use serde_bytes::ByteBuf;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct HttpServerRequest {
pub headers: Vec<HttpServerKeyValuePair>,
pub params: Vec<HttpServerKeyValuePair>,
pub query: Vec<HttpServerKeyValuePair>,
pub body: Option<ByteBuf>,
Expand All @@ -24,6 +25,7 @@ impl HttpServerRequest {

pub fn new() -> HttpServerRequest {
HttpServerRequest {
headers: vec![],
params: vec![],
query: vec![],
body: None,
Expand Down
6 changes: 6 additions & 0 deletions examples/http-server-app/src/wrap/imported/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ pub mod http_server_key_value_pair;
pub use http_server_key_value_pair::*;
pub mod http_server_request;
pub use http_server_request::*;
pub mod multipart_file_info;
pub use multipart_file_info::*;
pub mod multipart_key_value_pair;
pub use multipart_key_value_pair::*;
pub mod http_server_http_method;
pub use http_server_http_method::*;
pub mod http_server_module;
pub use http_server_module::*;
pub mod multipart_module;
pub use multipart_module::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use serde::{Serialize, Deserialize};
use polywrap_msgpack_serde::{
wrappers::polywrap_json::JSONString,
wrappers::polywrap_bigint::BigIntWrapper
};
use polywrap_wasm_rs::{
BigInt,
BigNumber,
Map,
JSON
};
use serde_bytes::ByteBuf;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MultipartFileInfo {
pub name: String,
pub content: ByteBuf,
}

impl MultipartFileInfo {
pub const URI: &'static str = "wrap://http/http.wrappers.dev/u/test/multipart";

pub fn new() -> MultipartFileInfo {
MultipartFileInfo {
name: String::new(),
content: ByteBuf::from(vec![]),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use serde::{Serialize, Deserialize};
use polywrap_msgpack_serde::{
wrappers::polywrap_json::JSONString,
wrappers::polywrap_bigint::BigIntWrapper
};
use polywrap_wasm_rs::{
BigInt,
BigNumber,
Map,
JSON
};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MultipartKeyValuePair {
pub key: String,
pub value: String,
}

impl MultipartKeyValuePair {
pub const URI: &'static str = "wrap://http/http.wrappers.dev/u/test/multipart";

pub fn new() -> MultipartKeyValuePair {
MultipartKeyValuePair {
key: String::new(),
value: String::new(),
}
}
}
45 changes: 45 additions & 0 deletions examples/http-server-app/src/wrap/imported/multipart_module/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use serde::{Serialize, Deserialize};
use polywrap_msgpack_serde::{
from_slice,
to_vec,
wrappers::polywrap_json::JSONString,
wrappers::polywrap_bigint::BigIntWrapper
};
use polywrap_wasm_rs::{
BigInt,
BigNumber,
Map,
JSON,
subinvoke
};
use serde_bytes::ByteBuf;
use crate::MultipartKeyValuePair;
use crate::MultipartFileInfo;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArgsGetFiles {
pub headers: Vec<MultipartKeyValuePair>,
pub body: ByteBuf,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MultipartModule {}

impl MultipartModule {
pub const URI: &'static str = "wrap://http/http.wrappers.dev/u/test/multipart";

pub fn new() -> MultipartModule {
MultipartModule {}
}

pub fn get_files(args: &ArgsGetFiles) -> Result<Vec<MultipartFileInfo>, String> {
let uri = MultipartModule::URI;
let args = to_vec(args).map_err(|e| e.to_string())?;
let result = subinvoke::wrap_subinvoke(
uri,
"getFiles",
args,
)?;
from_slice(result.as_slice()).map_err(|e| e.to_string())
}
}
7 changes: 6 additions & 1 deletion examples/http-server-app/src/wrap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ pub use imported::http_server_start_result::HttpServerStartResult;
pub use imported::http_server_response::HttpServerResponse;
pub use imported::http_server_key_value_pair::HttpServerKeyValuePair;
pub use imported::http_server_request::HttpServerRequest;
pub use imported::multipart_file_info::MultipartFileInfo;
pub use imported::multipart_key_value_pair::MultipartKeyValuePair;
pub use imported::http_server_http_method::{
get_http_server_http_method_key,
get_http_server_http_method_value,
sanitize_http_server_http_method_value,
HttpServerHttpMethod
};
pub use imported::http_server_module::HttpServerModule;
pub use imported::multipart_module::MultipartModule;
pub mod module;
pub use module::{
Module,
Expand All @@ -29,7 +32,9 @@ pub use module::{
route_with_query_wrapped,
ArgsRouteWithQuery,
route_post_wrapped,
ArgsRoutePost
ArgsRoutePost,
route_upload_wrapped,
ArgsRouteUpload
};

// Override print!(...) & println!(...) macros
Expand Down
4 changes: 3 additions & 1 deletion examples/http-server-app/src/wrap/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ pub use wrapped::{
route_with_query_wrapped,
ArgsRouteWithQuery,
route_post_wrapped,
ArgsRoutePost
ArgsRoutePost,
route_upload_wrapped,
ArgsRouteUpload
};

pub mod module;
Expand Down
3 changes: 3 additions & 0 deletions examples/http-server-app/src/wrap/module/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
ArgsRouteWithParam,
ArgsRouteWithQuery,
ArgsRoutePost,
ArgsRouteUpload,
};
use crate::HttpServerRequest;
use crate::HttpServerResponse;
Expand All @@ -33,4 +34,6 @@ pub trait ModuleTrait {
fn route_with_query(args: ArgsRouteWithQuery) -> Result<HttpServerResponse, String>;

fn route_post(args: ArgsRoutePost) -> Result<HttpServerResponse, String>;

fn route_upload(args: ArgsRouteUpload) -> Result<HttpServerResponse, String>;
}
Loading

0 comments on commit c3f1e9a

Please sign in to comment.