Skip to content

Commit

Permalink
feature: Upgrade the wasm demo developed by rust(#255) (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjingboy authored Apr 22, 2022
1 parent 78a4d88 commit cd96c75
Show file tree
Hide file tree
Showing 15 changed files with 464 additions and 252 deletions.
15 changes: 15 additions & 0 deletions demo/faas/code/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Cargo
# will have compiled files and executables
**/target/
# These are backup files generated by rustfmt
**/*.rs.bk

.DS_Store

# The cache for docker container dependency
.cargo

# The cache for chain data in container
.local

.idea/
7 changes: 7 additions & 0 deletions demo/faas/code/rust/.rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
format_strings = true
group_imports = "StdExternalCrate"
hex_literal_case = "Lower"
imports_granularity = "Crate"
reorder_impl_items = true
use_field_init_shorthand = true
wrap_comments = true
21 changes: 21 additions & 0 deletions demo/faas/code/rust/Cargo.lock

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

6 changes: 6 additions & 0 deletions demo/faas/code/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.release]
panic = "abort"
lto = true

[workspace]
members = ["./client", "./proxy-wasm", "./server"]
5 changes: 5 additions & 0 deletions demo/faas/code/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Build

* `cargo build --target=wasm32-unknown-unknown --release`
* Copy `target/wasm32-unknown-unknown/release/function_1.wasm` and `target/wasm32-unknown-unknown/release/function_2.wasm` to your actual directory.
* Test the demo follow [https://mosn.io/layotto/#/zh/start/faas/start](https://mosn.io/layotto/#/zh/start/faas/start).
13 changes: 13 additions & 0 deletions demo/faas/code/rust/client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "client"
version = "0.1.0"
edition = "2021"

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
proxy-wasm = { path = "../proxy-wasm" }
81 changes: 81 additions & 0 deletions demo/faas/code/rust/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::str;

use proxy_wasm::{
dispatcher, get_buffer, invoke_service, log, set_buffer,
traits::{Context, DefaultRootContext, HttpContext, RootContext},
types::*,
};

fn get_query_param(body: &str, param_name: &str) -> Option<String> {
for item in body.split("&") {
let key = format!("{:}=", param_name);
if item.starts_with(&key) {
return Some((&item[key.len()..]).to_owned());
}
}
None
}

fn get_book_name(body: &Option<Bytes>) -> Option<String> {
match body {
Some(body) => match str::from_utf8(body) {
Ok(body) => get_query_param(body, "name"),
_ => None,
},
None => None,
}
}

#[no_mangle]
pub fn _start() {
dispatcher::set_root_context(|_| -> Box<dyn RootContext> {
Box::new(DefaultRootContext::<ClientHttpContext>::default())
});
}

#[no_mangle]
#[allow(unused_must_use)]
pub extern "C" fn proxy_get_id() {
set_buffer(BufferType::BufferTypeCallData, 0, b"id_1");
}

#[derive(Default)]
struct ClientHttpContext {}

impl Context for ClientHttpContext {}

#[allow(unused_must_use)]
impl HttpContext for ClientHttpContext {
fn on_http_request_body(&mut self, body_size: usize, _end_of_stream: bool) -> Action {
let book_name = get_buffer(BufferType::HttpRequestBody, 0, body_size)
.map_or(None, |buffer| get_book_name(&buffer));
match book_name {
Some(book_name) => match invoke_service("id_2", "", &book_name) {
Ok(response) => {
let response = response.map_or("".to_string(), |v| {
String::from_utf8(v).unwrap_or("".to_string())
});
set_buffer(
BufferType::HttpResponseBody,
0,
format!("There are {:} inventories for {:}.\n", response, book_name)
.as_bytes(),
)
.unwrap();
Action::Continue
}
Err(status) => {
log(
LogLevel::Error,
&format!("Invoke service failed: {}", status as u32),
);
Action::Pause
}
},
None => {
log(LogLevel::Error, "Param 'name' not found");
Action::Pause
}
}
}
}
225 changes: 0 additions & 225 deletions demo/faas/code/rust/lib.rs

This file was deleted.

8 changes: 8 additions & 0 deletions demo/faas/code/rust/proxy-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "proxy-wasm"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Loading

0 comments on commit cd96c75

Please sign in to comment.