-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from polywrap/develop
Release 0.2.5
- Loading branch information
Showing
70 changed files
with
12,364 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!src/wrap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v16.13.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.