A serverless server with wasmer and WebAssembly
APACHE-2.0 License
Lego is a serverless service with WebAssembly and wasmer.
export function helloWorld() {
return "Hello, Lego";
}
Lego
where to get/set bytes into memory by defining and exporting memory_ptr
function:let memory = new ArrayBuffer(1024);
export function memory_ptr(): usize {
return changetype<usize>(memory);
}
// Help for set string into memory
function saveStringIntoMemory(data: String): usize {
const len = raw.length;
let view = new DataView(memory);
for (let i=0;i<(len as i32);i++) {
view.setUint8(i, raw.charCodeAt(i) as i8);
}
return len;
}
http_get
hook to Lego
:export function http_get(inputLen: usize): usize {
return saveStringIntoMemory(helloWorld());
}
$ npm run build
$ cp ./build/helloworld.wasm /path/to/wasm/helloworld.wasm
Lego
LEGO_WASM_ROOT
as the root locatoin of your wasms.export LEGO_WASM_ROOT="/path/to/wasm"
$ /path/to/lego
Lego
$ curl http://localhost:8000/helloworld | jq # `helloworld` is the name of your wasm file without extension
{
"result": "hello world"
}
You can find more AssemblyScript examples written in assemblyscripts.