Skip to content

Commit 548a1f0

Browse files
committed
tests(*): add JSON Schema and demo script
1 parent eb52bcc commit 548a1f0

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "proxy-wasm-rust-my-filter"
2+
name = "my-rust-filter"
33
version = "0.1.0"
44
authors = ["Caio Ramos Casimiro <caiorcasimiro@gmail.com>"]
55
edition = "2018"

my_rust_filter.meta.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"config_schema": {
3+
"type": "object",
4+
"properties": {
5+
"my_greeting": { "type": "string" }
6+
}
7+
}
8+
}

test/config/demo.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
_format_version: "1.1"
3+
4+
services:
5+
- name: demo
6+
url: http://httpbin.org
7+
# url: http://host.docker.internal:6502
8+
routes:
9+
- name: my-route
10+
paths:
11+
- /
12+
strip_path: false
13+
filter_chains:
14+
- filters:
15+
- name: my_rust_filter
16+
config:
17+
my_greeting: "Hello from Rust on Wasm!"
18+

test/demo.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
set -x
3+
4+
DEMO_KONG_CONTAINER="${DEMO_KONG_CONTAINER:-kong-wasm}"
5+
DEMO_KONG_IMAGE="${DEMO_KONG_IMAGE:-kong/kong:nightly}"
6+
7+
function message() {
8+
set +x
9+
echo "----------------------------------------------------------------------"
10+
echo $1
11+
echo "----------------------------------------------------------------------"
12+
set -x
13+
}
14+
15+
################################################################################
16+
17+
if [[ "$1" == "stop" ]]
18+
then
19+
docker stop $DEMO_KONG_CONTAINER
20+
docker rm $DEMO_KONG_CONTAINER
21+
exit 0
22+
fi
23+
24+
### Build filter ###############################################################
25+
26+
message "Building the filter using cargo..."
27+
28+
(
29+
cd ..
30+
cargo build --target=wasm32-wasi --release || exit 1
31+
) || exit 1
32+
33+
### Copy filter to wasm/ #######################################################
34+
35+
mkdir -p wasm
36+
37+
cp -a ../target/wasm32-wasi/release/*.wasm ../*.meta.json wasm/
38+
39+
script_dir=$(dirname $(realpath $0))
40+
41+
### Start container ############################################################
42+
43+
message "Setting up the Kong Gateway container..."
44+
45+
docker stop $DEMO_KONG_CONTAINER
46+
docker rm $DEMO_KONG_CONTAINER
47+
48+
# Config trick to access localhost in a local Docker test,
49+
# in case you want to edit your config/demo.yml to target
50+
# a localhost server rather than httpbin.org:
51+
#
52+
# access_localhost="--add-host=host.docker.internal:$(ip -j address | jq -r '[ .[] | select(.ifname | test("^[ew]")) | .addr_info[] | select(.family == "inet") | .local ][0]')"
53+
access_localhost=""
54+
55+
docker run -d --name "$DEMO_KONG_CONTAINER" \
56+
$access_localhost \
57+
-v "$script_dir/config:/kong/config/" \
58+
-v "$script_dir/wasm:/wasm" \
59+
-e "KONG_LOG_LEVEL=info" \
60+
-e "KONG_DATABASE=off" \
61+
-e "KONG_DECLARATIVE_CONFIG=/kong/config/demo.yml" \
62+
-e "KONG_NGINX_WASM_SHM_KV_KONG_WASM_RATE_LIMITING_COUNTERS=12m" \
63+
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
64+
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
65+
-e "KONG_WASM=on" \
66+
-e "KONG_WASM_FILTERS_PATH=/wasm" \
67+
-p 8000:8000 \
68+
-p 8443:8443 \
69+
-p 8001:8001 \
70+
-p 8444:8444 \
71+
"$DEMO_KONG_IMAGE"
72+
73+
### Show configuration #########################################################
74+
75+
message "This is the configuration loaded into Kong:"
76+
77+
cat config/demo.yml
78+
79+
sleep 5
80+
81+
### Issue requests #############################################################
82+
83+
message "Now let's send a request to see the filter in effect:"
84+
85+
http :8000/anything
86+
87+
message "Finishing up!"
88+
89+
docker stop $DEMO_KONG_CONTAINER
90+
#docker rm $DEMO_KONG_CONTAINER
91+

0 commit comments

Comments
 (0)