Skip to content

Custom Spring Cloud Gateway filter that calls a WebAssembly function written in C from a .wasm file

License

Notifications You must be signed in to change notification settings

Albertoimpl/wasm-gateway-filters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Cloud Gateway and WebAssembly

Creating a custom Spring Cloud Gateway filter that calls a WebAssembly function written in C from a .wasm file using wasmtime-java.

To run the project:

./gradlew bootRun

To call the function:

curl localhost:8081/wasm -H"X-CustomSum-x:10" -H"X-CustomSum-y:16" -sI | grep X-CustomSum

 
X-CustomSum: 26

This request will call a custom filter that will sum two digits and add it to the response headers:

spring:
  cloud:
    gateway:
      routes:
        - uri: https://albertoimpl.com
          predicates:
            - Path=/wasm
          filters:
            - StripPrefix=1
            - CustomSumWASMFilter

To compile the sum function:

git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
emcc sum/sum.c -o sum/sum.js -s EXPORTED_FUNCTIONS='["_sum"]'
cp sum/sum.wasm src/main/resources/

About

Custom Spring Cloud Gateway filter that calls a WebAssembly function written in C from a .wasm file

Topics

Resources

License

Stars

Watchers

Forks