-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
78a4d88
commit cd96c75
Showing
15 changed files
with
464 additions
and
252 deletions.
There are no files selected for viewing
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,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/ |
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,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 |
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
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"] |
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 @@ | ||
# 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). |
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,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" } |
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,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 | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
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
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] |
Oops, something went wrong.