• Stars
    star
    346
  • Rank 122,430 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A template project for building a database-driven microservice in Rust and run it in the WasmEdge sandbox.

Secure & lightweight microservice with a database backend

In this repo, we demonstrate a microservice written in Rust, and connected to a MySQL database. It supports CRUD operations on a database table via a HTTP service interface. The microservice is compiled into WebAssembly (Wasm) and runs in the WasmEdge Runtime, which is a secure and lightweight alternative to natively compiled Rust apps in Linux containers. The WasmEdge Runtime can be managed and orchestrated by container tools such as the Docker, Podman, as well as almost all flavors of Kubernetes. It also works with microservice management frameworks such as Dapr. Checkout this article or this video to learn how the Rust code in this microservice works.

Quickstart with Docker

The easiest way to get started is to use a version of Docker Desktop or Docker CLI with Wasm support.

Then, you just need to type one command.

export BUILDX_NO_DEFAULT_ATTESTATIONS=1
docker compose up

This will build the Rust source code, run the Wasm server, and startup a MySQL backing database. It also starts a basic STATIC web interface (available at http://localhost:8090). See the Dockerfile and docker-compose.yml files. You can jump directly to the CRUD tests section to interact with the web service.

However, if you want to build and run the microservice app step by step on your own system. Follow the detailed instructions below.

Prerequisites

On Linux, you can use the following commands to install Rust and WasmEdge.

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
# Install WebAssembly target for Rust
rustup target add wasm32-wasi

# Install WasmEdge
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -e all
source $HOME/.wasmedge/env

Build

Use the following command to build the microservice. A WebAssembly bytecode file (wasm file) will be created.

cargo build --target wasm32-wasi --release

You can run the AOT compiler on the wasm file. It could significantly improvement the performance of compute-intensive applications. This microservice, however, is a network intensitive application. Our use of async HTTP networking (Tokio and hyper) and async MySQL connectors are crucial for the performance of this microservice.

wasmedgec target/wasm32-wasi/release/order_demo_service.wasm order_demo_service.wasm

Run

You can use the wasmedge command to run the wasm application. It will start the server. Make sure that you pass the MySQL connection string as the env variable to the command.

wasmedge --env "DATABASE_URL=mysql://user:[email protected]:3306/mysql" order_demo_service.wasm

CRUD tests

Open another terminal, and you can use the curl command to interact with the web service.

When the microservice receives a GET request to the /init endpoint, it would initialize the database with the orders table.

curl http://localhost:8080/init

When the microservice receives a POST request to the /create_order endpoint, it would extract the JSON data from the POST body and insert an Order record into the database table. For multiple records, use the /create_orders endpoint and POST a JSON array of Order objects.

curl http://localhost:8080/create_orders -X POST -d @orders.json

When the microservice receives a GET request to the /orders endpoint, it would get all rows from the orders table and return the result set in a JSON array in the HTTP response.

curl http://localhost:8080/orders

When the microservice receives a POST request to the /update_order endpoint, it would extract the JSON data from the POST body and update the Order record in the database table that matches the order_id in the input data.

curl http://localhost:8080/update_order -X POST -d @update_order.json

When the microservice receives a GET request to the /delete_order endpoint, it would delete the row in the orders table that matches the id GET parameter.

curl http://localhost:8080/delete_order?id=2

That's it. Feel free to fork this project and use it as a template for your own lightweight microservices!

More Repositories

1

wasmedge-quickjs

A high-performance, secure, extensible, and OCI-complaint JavaScript runtime for WasmEdge.
JavaScript
487
star
2

wasm-learning

Building Rust functions for Node.js to take advantage of Rust's performance, WebAssembly's security and portability, and JavaScript's ease-of-use. Demo code and recipes.
Rust
430
star
3

SOLL

SOLL is a new compiler for generate Ewasm from solidity and yul. See a demo here: https://asciinema.org/a/ezJqNLicn5fya02zwu4VXIo8a
C++
376
star
4

buidl

A browser-based IDE for creating, deploying, and sharing blockchain apps (DApps, or decentralized apps). Publish your first blockchain DApps in 5 minutes! Here is how: https://docs.secondstate.io/buidl-developer-tool/getting-started
Vue
364
star
5

dapr-wasm

A template project to demonstrate how to run WebAssembly functions as sidecar microservices in dapr
Rust
275
star
6

smart-contract-search-engine

Takes a link to a smart contract's raw ABI file and an RPC URL and then indexes all instances of that smart contract
JavaScript
267
star
7

WasmEdge-WASINN-examples

Rust
237
star
8

LlamaEdge

The easiest & fastest way to run customized and fine-tuned LLMs locally or on the edge
Rust
206
star
9

wasmedge-nodejs-starter

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

rustwasmc

Tool for building Rust functions for Node.js. Combine the performance of Rust, safety and portability of WebAssembly, and ease of use of JavaScript.
Rust
129
star
11

how_to_deploy_uniswap

Detailed instructions on how to deploy Uniswap on an Ethereum compatible blockchain
JavaScript
124
star
12

chat-with-chatgpt

Chat with ChatGPT via GitHub issue comments.
111
star
13

WasmEdge-go

The GO language SDK and API for WasmEdge
Go
107
star
14

vercel-wasm-runtime

A template project for building high-performance, portable, and safe serverless functions in Vercel.
JavaScript
81
star
15

wasmedge-containers-examples

Shell
71
star
16

wasmedge_wasi_socket

A Rust lib for socket on WasmEdge.
Rust
70
star
17

meetups

70
star
18

rust-by-example-ext

Rust by Example -- Extended Edition
Dockerfile
64
star
19

lity

A rule-based contract-oriented high-level language.
C++
56
star
20

wasm-joey

Serverless Wasm - A lightweight Node.js application for deploying and executing WebAssembly(Wasm) binary-code via HTTP
JavaScript
54
star
21

ssvm-deno-starter

A template project to run Rust functions in Deno through the Second State WebAssembly engine.
Dockerfile
53
star
22

SewUp

A library to help you sew up your Ethereum project with Rust and just like develop in a common backend
Rust
52
star
23

rust-wasm-ai-demo

Rust functions for Tensorflow inference in Node.js. Rust's performance, WebAssembly's security and portability, and Javascript's ease-of-use.
Dockerfile
49
star
24

WasmEdge-go-examples

Go
46
star
25

recrypt-as-a-service

An open key management service to support privacy-first and scalable file sharing.
Rust
46
star
26

learn-rust-with-github-actions

Getting started with the Rust programming language using this VSCode and GitHub Actions template.
Dockerfile
46
star
27

aws-lambda-wasm-runtime

A template project for building high-performance, portable, and safe serverless functions in AWS Lambda.
JavaScript
44
star
28

wasmedge-seL4

Integrate WasmEdge with seL4
Shell
42
star
29

dapr-sdk-wasmedge

An experimental Dapr SDK in Rust. It is designed to run in WasmEdge applications.
Rust
41
star
30

MEGA

Make ETLs Great Again!
Rust
40
star
31

tencent-tensorflow-scf

A template project for serverless functions for Tensorflow inference on Tencent Cloud.
HTML
38
star
32

simple-staking-smart-contract

A smart contract which allows users to stake and un-stake (after a set period of time) a specified ERC20 token.
Solidity
38
star
33

wasm32-wasi-benchmark

C++
35
star
34

WebAssembly-landscape

The current development of WebAssembly
34
star
35

rust-examples

Simple Rust applications that run in WasmEdge
Rust
32
star
36

wasmedge-rustsdk-examples

Rust
31
star
37

tencent-scf-wasm-runtime

基于 WebAssembly 容器镜像的高性能腾讯云函数开发模版。A template project for building high-performance, portable, and safe serverless functions in Tencent Serverless Cloud Functions.
JavaScript
29
star
38

crunw

Add WasmEdge support to crun so that Docker and k8s tools can manage WasmEdge runtimes side by side with Docker-like application containers.
C
29
star
39

wasmedge-bindgen

Let WebAssembly's exported function support more data types for its parameters and return values.
Rust
29
star
40

wasmedge_tensorflow_interface

Rust
20
star
41

kubecon-eu-2023

20
star
42

substrate-ewasm

A substrate runtime module library (SRML) for Etherem flavored WebAssembly (ewasm)
18
star
43

runw

C++
18
star
44

simple-timelock-smart-contract

A simple timelock smart contract which locks ERC20 tokens for a specific time period and then allows users to unlock when that time period has elapsed
Solidity
17
star
45

netlify-wasm-runtime

A template project for building high-performance, portable, and safe serverless functions in Netlify.
JavaScript
16
star
46

rust-ssvm

Use EVMC binding SSVM and host written in Rust
Rust
15
star
47

serverless-reactor-starter

Rust
15
star
48

hugo-website

A no-code, no-software and no-cost solution to publishing sophisticated web sites managed by non-technical people.
HTML
15
star
49

witc

wasm-interface-types supplement & compiler of wasmedge
Haskell
13
star
50

ssvm-napi

SSVM Node.js Addon
C++
12
star
51

WasmEdge-tensorflow

C++
12
star
52

AI-as-a-Service

Rust
12
star
53

OCR-tesseract-on-Centos7

How to install and use tesseract OCR on Centos7 - without root access
11
star
54

interest-earner-smart-contract

A Solidity smart contract which allows users to stake, earn and un-stake.
Solidity
10
star
55

linear-timelock-smart-contract

Solidity smart contract which disburses ERC20 tokens linearly, over a specific period of time (all values are dynamic and are set by the contract owner)
Solidity
10
star
56

substrate-wasmedge

Substrate on WasmEdge
Rust
10
star
57

ssvm-tencent-starter

Shell
9
star
58

nodejs-helper

Rust
9
star
59

WasmEdge-tensorflow-tools

C++
9
star
60

llm-web-api-server

Rust
8
star
61

wasm-llm

Python
6
star
62

WasmEdge-evmc

Second State WebAssembly VM for EVMC Extension
C++
6
star
63

wasmedge-core

WasmEdge Node.js Addon
C++
6
star
64

wasmedge_plugin_rust_sdk

Rust
5
star
65

tencent-meme-scf

HTML
5
star
66

chatbot-ui

TypeScript
5
star
67

WasmEdge-image

C
5
star
68

qdrant-rest-client

A lightweight Qdrant client library for Rust
Rust
4
star
69

llm_todo

Python
4
star
70

wasmedge-mysql

A repository which provides a MySQL persistent storage solution for the WasmEdge runtime
JavaScript
4
star
71

SSVMRPC

A Remote Procedure Call (RPC) implementation which facilitates both code-deployment and code-execution interactions with SecondState's stateless Virtual Machine (SSVM)
Rust
4
star
72

opendapps

HTML
4
star
73

ssvm-napi-extensions

C++
4
star
74

micro-slots

Storing and accessing multiple integer values in Ethereum, using only one single uint256 slot
Python
4
star
75

libeni

Official C++ implementation of libENI, which is part of the Lity project.
C++
4
star
76

serialize_deserialize_u8_i32

A Rust library that safely converts, back and forward, between u8 and i32
Rust
3
star
77

tencent-ocr-scf

Serverless functions for OCR on Tencent Cloud.
HTML
3
star
78

ssvm-tencent-tensorflow

HTML
3
star
79

wasmedge_hostfunctionexample_interface

This is an example to create a host function interface for calling the WasmEdge internal host functions.
Rust
3
star
80

wasmmark

JavaScript
3
star
81

wasmedge-extensions

WasmEdge for Node.js Addon with extensions
C++
3
star
82

server-side-wasm-video-editing

An example of how to build a third-party application that uses SecondState's Wasm VM to edit video on the server side. This prototype shows how video can undergo frame-by-frame, pixel-by-pixel processing using native Wasm data only i.e. pixel as i32
HTML
3
star
83

docai-hf-rs

Document AI for WasmEdge
2
star
84

linkerd-wasm

A template project to run WebAssembly functions as microservices in Linkerd
2
star
85

wasmedge_process_interface

Rust
2
star
86

rfq-marketplace

Rust
2
star
87

WASI-NN-GGML-PLUGIN-REGISTRY

This is a registry project for storing all released WasmEdge WASI-NN GGML plugin with different llama.cpp versions.
2
star
88

linear-timelock-user-interface

Interface for LinearTimelock.sol smart contract
JavaScript
2
star
89

rust-faas-showcases

HTML
2
star
90

rust_native_storage_library

A library which compiles to `.so` and `.dylib` and facilitates the native storage (and retrieval) of key:value pairs on the host system
Rust
2
star
91

WasmEdge-storage

Second State WebAssembly VM for Rust Storage Extension
C++
2
star
92

simple-timelock-user-interface

A user interface for the simple timelock smart contract
JavaScript
2
star
93

WasmEdge-paratime

An Oasis Paratime using WasmEdge
Rust
2
star
94

SSVMContainer

A Rust application that sits between incoming requests from the network and the SSVM. This application handles the deployment of Wasm applications and manages the execution of services (callable functions inside the Wasm application). The actual execution takes place inside the SSVM. The SSVM is a stack-based Virtual Machine which is stateless. This SSVMContainer application is responsible for fielding incoming deployments and service calls as well as storing application state.
HTML
2
star
95

wasmedge_rustls_api

Rust
1
star
96

libsql_wasi_nn

Rust
1
star
97

haiku-connector

Haiku Connector Runner
Rust
1
star
98

wasmedge_hyper_rustls

A crate similar to rustls-hyper for wasi.
Rust
1
star
99

wasmedge_asyncify

Rust
1
star
100

interest-earner-user-interface

A user interface for the interest earner smart contract
JavaScript
1
star