GithubHelp home page GithubHelp logo

zw007 / ssvm-nodejs-starter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from second-state/wasmedge-nodejs-starter

0.0 0.0 0.0 744 KB

A template project to run Rust functions in Node.js through the Second State WebAssembly engine.

Home Page: https://www.secondstate.io/articles/getting-started-with-rust-function/

JavaScript 20.07% Rust 5.73% Dockerfile 74.20%

ssvm-nodejs-starter's Introduction

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 ssvmup 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 Docker edition. Check out the Github Codespaces edition here.

Docker

Build the WebAssembly module.

$ docker pull secondstate/ssvm-nodejs-starter
$ docker run -p 3000:3000 --rm -it -v $(pwd):/app secondstate/ssvm-nodejs-starter
(docker) # cd /app
(docker) # ssvmup build

Option 1: Upload to the FaaS and test

Upload the wasm file in the pkg folder to the FaaS. Double check the .wasm file name before you upload.

(docker) # 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 first terminal window, start the Node.js application.

(docker) # node node/app.js

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

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

More exercises

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

Read more:

Resources

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

ssvm-nodejs-starter's People

Contributors

alabulei1 avatar hydai avatar juntao avatar vrixyz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.