|
| 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