https://github.com/shonenada/lego
A serverless server with wasmer and WebAssembly
https://github.com/shonenada/lego
rust rust-lang serverless wasmer webassembly
Last synced: 5 months ago
JSON representation
A serverless server with wasmer and WebAssembly
- Host: GitHub
- URL: https://github.com/shonenada/lego
- Owner: shonenada
- License: apache-2.0
- Created: 2020-02-24T18:16:28.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-23T16:14:58.000Z (over 5 years ago)
- Last Synced: 2025-04-23T23:48:54.752Z (11 months ago)
- Topics: rust, rust-lang, serverless, wasmer, webassembly
- Language: Rust
- Homepage:
- Size: 61.5 KB
- Stars: 13
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Lego
====
Lego is a serverless service with [WebAssembly](https://webassembly.org/) and [wasmer](https://wasmer.io).
## Quick Guide
### Prepare wasm
1. Write your wasm in any language you like, in AssemblyScript for example:
```js
export function helloWorld() {
return "Hello, Lego";
}
```
2. Tell `Lego` where to get/set bytes into memory by defining and exporting `memory_ptr` function:
```js
let memory = new ArrayBuffer(1024);
export function memory_ptr(): usize {
return changetype(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;
}
```
3. Export `http_get` hook to `Lego`:
```
export function http_get(inputLen: usize): usize {
return saveStringIntoMemory(helloWorld());
}
```
4. Compiles into wasm and store in any path you like:
```sh
$ npm run build
$ cp ./build/helloworld.wasm /path/to/wasm/helloworld.wasm
```
### Run `Lego`
1. Setup environment varialble `LEGO_WASM_ROOT` as the root locatoin of your wasms.
```
export LEGO_WASM_ROOT="/path/to/wasm"
```
2. Here we go ~
```
$ /path/to/lego
```
### Request `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](./assemblyscripts/assembly).