Skip to content

Latest commit

 

History

History
109 lines (73 loc) · 5.7 KB

README-Codespaces.md

File metadata and controls

109 lines (73 loc) · 5.7 KB

Getting started

Build and test

Fork this project to create your own Rust functions as a web service.

How it works

  • The Rust function source code is in the src/lib.rs file.
  • Use the rustwasmc tool to compile the Rust function into a WebAssembly module in the pkg directory.
  • Optional: Use the node node/app.js command to test the function locally in Node.js.
  • Upload the pkg/*.wasm file to the Second State FaaS service, OR to a Node.js server, to turn it into a web service.

This is the Github Codespaces edition. Check out the Docker edition here.

Github Codespaces

GitHub Codespaces

Click on the Code -> Open with Codespaces menu, and wait for the Codespaces IDE screen to build and come up. This could take a few minutes as it installs all the dependencies Rust and WebAssembly requires.

Open the src/lib.rs and Cargo.toml files to learn about the Rust function.

Open the Terminal window via the Terminal -> New Terminal menu, and run the command and wait until it completes building.

$ rustwasmc build

Option 1: Upload to the FaaS and test

Run the following command in the terminal window. Upload the wasm file in the pkg folder to the FaaS. Double check the .wasm file name before you upload.

$ curl --location --request POST 'https://rpc.ssvm.secondstate.io:8081/api/executables' \
--header 'Content-Type: application/octet-stream' \
--header 'SSVM-Description: say hello' \
--data-binary '@pkg/hello_lib_bg.wasm'

The FaaS returns

{"wasm_id":161,"wasm_sha256":"0xfb413547a8aba56d0349603a7989e269f3846245e51804932b3e02bc0be4b665","usage_key":"00000000-0000-0000-0000-000000000000","admin_key":"00xxxxxx-xxxx-xxxx-xxxx-4adc960fd2b8"}

Make a function call via the web.

(docker) # curl --location --request POST 'https://rpc.ssvm.secondstate.io:8081/api/run/161/say' \
--header 'Content-Type: text/plain' \
--data-raw 'Second State FaaS'
hello Second State FaaS

You can easily incorporate this web service into your HTML web pages. See how

Option 2: Local test and debug

From the terminal window, start the Node.js application.

(docker) # node node/app.js

Create a second terminal window, you can test the local server.

$ curl http://localhost:3000/?name=WasmEdge
hello WasmEdge

More exercises

Now, you can copy and paste code from this project.

Build, deploy, test. You can find many more examples to play with here:

Read more:

Resources

Brought to you by the Open source dev team at Second State. Follow us on Twitter, Facebook, LinkedIn, YouTube, or Medium