Skip to content

Commit

Permalink
Merge pull request #42 from polywrap/develop
Browse files Browse the repository at this point in the history
Release 0.2.5
  • Loading branch information
nerfZael authored Sep 10, 2023
2 parents 9fb03d7 + 5d09515 commit f8c5e1a
Show file tree
Hide file tree
Showing 70 changed files with 12,364 additions and 30 deletions.
47 changes: 46 additions & 1 deletion Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ members = [
"examples/simple-pwr-app",
"examples/http-server-app",
"examples/simple-storage-app",
"examples/wrap-link-deployer",
"examples/advanced-server-app"
]

default-members = [
Expand All @@ -36,6 +38,8 @@ default-members = [
"examples/simple-pwr-app",
"examples/http-server-app",
"examples/simple-storage-app",
"examples/wrap-link-deployer",
"examples/advanced-server-app"
]

[workspace.dependencies]
Expand All @@ -46,6 +50,7 @@ wrap_utils_wasm = { path = "packages/wrap-utils-wasm" }
easy_error_string = { version = "0.1.0", path = "packages/easy-error-string" }
polywrap_http_server_plugin = { path = "packages/plugins/http-server" }
polywrap_key_value_store_plugin = { path = "packages/plugins/key-value-store" }
polywrap_msgpack_serde = "0.0.2-beta.7"
polywrap_uri = { git = "https://github.com/polywrap/rust-client", branch = "nerfzael-plugin-option-deser-issue" }
polywrap_core = { git = "https://github.com/polywrap/rust-client", branch = "nerfzael-plugin-option-deser-issue" }
polywrap_client = { git = "https://github.com/polywrap/rust-client", branch = "nerfzael-plugin-option-deser-issue" }
Expand All @@ -58,7 +63,7 @@ polywrap_wasm = { git = "https://github.com/polywrap/rust-client", branch = "ner
polywrap_fs_plugin = { git = "https://github.com/polywrap/rust-client", branch = "nerfzael-plugin-option-deser-issue" }
polywrap_http_plugin = { git = "https://github.com/polywrap/rust-client", branch = "nerfzael-plugin-option-deser-issue" }
polywrap-wasm-rs = { version = "0.11.2" }
polywrap_msgpack_serde = "0.0.2-beta.7"
tower-http = { version = "0.4.3", features = ["cors"] }
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"
serde_json = "1.0"
Expand Down
1 change: 1 addition & 0 deletions examples/advanced-server-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!src/wrap
1 change: 1 addition & 0 deletions examples/advanced-server-app/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.13.0
22 changes: 22 additions & 0 deletions examples/advanced-server-app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "advanced-server-app"
version = "0.1.0"
description = "An example of a simple PWR HTTP server application."

edition.workspace = true
rust-version.workspace = true
license.workspace = true

[dependencies]
polywrap_msgpack_serde.workspace = true
polywrap-wasm-rs.workspace = true
serde = { version = "1.0", features = ["derive"] }
serde_bytes.workspace = true

[lib]
crate-type = ["cdylib"]

[profile.release]
opt-level = 's'
lto = true
panic = 'abort'
2 changes: 2 additions & 0 deletions examples/advanced-server-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Advanced HTTP server app
An example of a PWR HTTP server application with key value storage.
61 changes: 61 additions & 0 deletions examples/advanced-server-app/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/./bin/sh
set -x
set -e
# Install the wasm32 rust build target
rustup target add wasm32-unknown-unknown

# Install the toml-cli
cargo install toml-cli

# Install wasm-snip
cargo install wasm-snip

# Install wasm-bindgen
cargo install wasm-bindgen-cli

# Install wasm-tools
cargo install wasm-tools

cargo install wasm-opt

# Ensure the module at {{dir}} has the crate-type = ["cdylib"]
# toml set ./Cargo.toml lib.crate-type ["cdylib"] > ./Cargo-local.toml && \
# mv ./Cargo.toml ./Cargo-bak.toml && \
# mv ./Cargo-local.toml ./Cargo.toml

# Clean up artifacts left by the toml CLI program ("["cdylib"]" -> ["cdylib"])
# sed -i .bak 's/"\[cdylib\]"/\["cdylib"\]/g' ./Cargo.toml && \
# rm -rf ./Cargo.toml.bak

# # Ensure the package name = "module"
# toml set ./Cargo.toml package.name "module" > ./Cargo-local.toml && \
# rm -rf ./Cargo.toml && \
# mv ./Cargo-local.toml ./Cargo.toml

# Ensure the Wasm module is configured to use imported memory
export RUSTFLAGS="-C link-arg=-z -C link-arg=stack-size=65536 -C link-arg=--import-memory"

# Build the module
cargo build --manifest-path Cargo.toml \
--target wasm32-unknown-unknown --release

# Enable the "WASM_INTERFACE_TYPES" feature, which will remove the __wbindgen_throw import.
# See: https://github.com/rustwasm/wasm-bindgen/blob/7f4663b70bd492278bf0e7bba4eeddb3d840c868/crates/cli-support/src/lib.rs#L397-L403
export WASM_INTERFACE_TYPES=1

# Run wasm-bindgen over the module, replacing all placeholder __wbindgen_... imports
wasm-bindgen ../../target/wasm32-unknown-unknown/release/advanced_server_app.wasm --out-dir ./bin --out-name bg_module.wasm

# Run wasm-tools strip to remove the wasm-interface-types custom section
wasm-tools strip ./bin/bg_module.wasm -d wasm-interface-types -o ./bin/strip_module.wasm
rm -rf ./bin/bg_module.wasm

# Run wasm-snip to trip down the size of the binary, removing any dead code
wasm-snip ./bin/strip_module.wasm -o ./bin/snipped_module.wasm
rm -rf ./bin/strip_module.wasm

# Use wasm-opt to perform the "asyncify" post-processing step over all modules
export ASYNCIFY_STACK_SIZE=65535
wasm-opt --asyncify -Os ./bin/snipped_module.wasm -o ./bin/wrap.wasm
rm -rf ./bin/snipped_module.wasm
cp ./bin/wrap.wasm ./build/wrap.wasm
16 changes: 16 additions & 0 deletions examples/advanced-server-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"private": true,
"scripts": {
"codegen": "npx polywrap codegen",
"compile": "./build.sh",
"build": "yarn codegen && yarn compile",
"deploy": "pwr deploy"
},
"devDependencies": {
"@types/jest": "26.0.8",
"jest": "26.6.3",
"polywrap": "0.11.2",
"ts-jest": "26.5.4",
"typescript": "4.9.5"
}
}
5 changes: 5 additions & 0 deletions examples/advanced-server-app/polywrap.docs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
format: 0.1.0
description: An example of a PWR HTTP server application with key value storage.
website: https://polywrap.io
repository: https://github.com/polywrap/pwr
readme: ./README.md
9 changes: 9 additions & 0 deletions examples/advanced-server-app/polywrap.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import { Module, Response, KeyValuePair, Request } into HttpServer from "wrap://https/http.wrappers.dev/u/test/http-server"
#import * into Multipart from "wrap://https/http.wrappers.dev/u/test/multipart"
#import { Module, KeyValuePair } into KeyValueStore from "wrap://https/http.wrappers.dev/u/test/key-value-store"

type Module {
main(args: [String!]!): Int32!
onStart: Boolean!
routeHome(request: HttpServer_Request!): HttpServer_Response!
}
9 changes: 9 additions & 0 deletions examples/advanced-server-app/polywrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
format: 0.4.0
project:
name: advanced-server-app
type: wasm/rust
source:
module: ./Cargo.toml
schema: ./polywrap.graphql
extensions:
docs: ./polywrap.docs.yaml
80 changes: 80 additions & 0 deletions examples/advanced-server-app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
pub mod wrap;

use polywrap_wasm_rs::wrap_debug_log;
use serde_bytes::ByteBuf;
pub use wrap::*;
use wrap::imported::*;

const DEFAULT_PORT: u16 = 8080;

impl ModuleTrait for Module {
fn main(
args: ArgsMain,
) -> Result<i32, String> {
let port = if args.args.len() > 0 {
args.args[0].parse::<u16>().unwrap_or(DEFAULT_PORT)
} else {
DEFAULT_PORT
};

log(format!("Starting server at {port}"));

HttpServerModule::start(&imported::http_server_module::ArgsStart {
port,
request_timeout: 10000,
routes: vec![
HttpServerRoute {
path: "/".to_string(),
http_method: HttpServerHttpMethod::GET,
handler: HttpServerWrapperCallback {
uri: "https/http.wrappers.dev/u/test/advanced-server-app".to_string(),
method: "routeHome".to_string()
}
},
],
on_start: Some(
HttpServerWrapperCallback {
uri: "https/http.wrappers.dev/u/test/advanced-server-app".to_string(),
method: "onStart".to_string()
}
),
}).unwrap();

Ok(0)
}

fn on_start(_: ArgsOnStart) -> Result<bool, String> {
log("Server started".to_string());

Ok(true)
}

fn route_home(_: ArgsRouteHome) -> Result<HttpServerResponse, String> {
log("Home route with counter");

let result = KeyValueStoreModule::get(&ArgsGet {
key: "counter".to_string(),
}).unwrap();

let counter = result.unwrap_or(ByteBuf::from(vec![0])).to_vec()[0];

KeyValueStoreModule::set(&ArgsSet {
key: "counter".to_string(),
value: ByteBuf::from(vec![counter + 1])
}).unwrap();

let resp = format!("Hey there namesty: {}", counter);
Ok(HttpServerResponse {
status_code: 200,
headers: Some(vec![HttpServerKeyValuePair {
key: "Content-Type".to_string(),
value: "text/html".to_string(),
}]),
body: Some(ByteBuf::from(resp.as_bytes().to_vec())),
})
}
}

fn log<S: Into<String>>(message: S) {
wrap_debug_log(&message.into());
}
37 changes: 37 additions & 0 deletions examples/advanced-server-app/src/wrap/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{
main_wrapped,
on_start_wrapped,
route_home_wrapped
};
use polywrap_wasm_rs::{
abort,
invoke,
InvokeArgs,
};

#[no_mangle]
pub extern "C" fn _wrap_invoke(method_size: u32, args_size: u32, env_size: u32) -> bool {
// Ensure the abort handler is properly setup
abort::wrap_abort_setup();

let args: InvokeArgs = invoke::wrap_invoke_args(method_size, args_size);
let result: Vec<u8>;

match args.method.as_str() {
"main" => {
result = main_wrapped(args.args.as_slice(), env_size);
}
"onStart" => {
result = on_start_wrapped(args.args.as_slice(), env_size);
}
"routeHome" => {
result = route_home_wrapped(args.args.as_slice(), env_size);
}
_ => {
invoke::wrap_invoke_error(format!("Could not find invoke function {}", args.method));
return false;
}
};
invoke::wrap_invoke_result(result);
return true;
}
Loading

0 comments on commit f8c5e1a

Please sign in to comment.