Skip to content

Commit

Permalink
Example WASM module that reverse the text from HTTP request
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Mar 1, 2024
1 parent 85fd6da commit 81108a2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Knative Sample Controller
# Knative WASM

[![GoDoc](https://godoc.org/github.com/cardil/knative-serving-wasm?status.svg)](https://godoc.org/github.com/cardil/knative-serving-wasm)
[![Go Report Card](https://goreportcard.com/badge/cardil/knative-serving-wasm)](https://goreportcard.com/report/cardil/knative-serving-wasm)

Knative `knative-serving-wasm` defines a few simple resources that are validated by
webhook and managed by a controller to demonstrate the canonical style in which
Knative writes controllers.
The `knative-serving-wasm` allows to run WASM modules that implement the
[wasi-http](https://github.com/WebAssembly/wasi-http) interface on Kubernetes.

To learn more about Knative, please visit our
[Knative docs](https://github.com/knative/docs) repository.
Expand Down
7 changes: 7 additions & 0 deletions examples/modules/reverse-text/Cargo.lock

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

3 changes: 2 additions & 1 deletion examples/modules/reverse-text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
querystring = "1.1.0"
querystring = "1.1"
urlencoding = "2.1"

[dependencies.wasi]
git = "https://github.com/bytecodealliance/wasi"
Expand Down
45 changes: 35 additions & 10 deletions examples/modules/reverse-text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,49 @@ impl exports::wasi::http::incoming_handler::Guest for Reverse {
}

/**
Get query parameter named "text", or return "Hello, WASI!" if
it's not present
*/
Get query parameter named "text", or return "Hello, WASI!" if
it's not present
*/
fn fetch_text_query_param(pq: String) -> String {
pq.split_once("?")
urlencoding::decode(&pq)
.unwrap()
.split_once("?")
.and_then(|(_, s)| {
return Some(querystring::querify(s))
return Some(querystring::querify(s));
})
.map(|q| {
HashMap::from_iter(q)
})
.map(|q| {
q.get("text")
.and_then(|q: HashMap<&str, &str>| {
q.get("text").cloned()
})
.unwrap_or(Some("Hello, WASI!".to_string()))
.unwrap()
.map(|s| {
s.to_string()
})
.unwrap_or("Hello, WASI!".to_string())
}

fn reverse_text(str: String) -> String {
str.chars().rev().collect()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_fetch_text_query_param() {
assert_eq!(fetch_text_query_param("".to_string()), "Hello, WASI!");
assert_eq!(fetch_text_query_param("?".to_string()), "Hello, WASI!");
assert_eq!(fetch_text_query_param("?text=Hello".to_string()), "Hello");
assert_eq!(fetch_text_query_param("?text=Happy%20testing".to_string()), "Happy testing");
}

#[test]
fn test_reverse_text() {
assert_eq!(reverse_text("".to_string()), "");
assert_eq!(reverse_text("a".to_string()), "a");
assert_eq!(reverse_text("ab".to_string()), "ba");
assert_eq!(reverse_text("abc".to_string()), "cba");
}
}

0 comments on commit 81108a2

Please sign in to comment.