Coder Social home page Coder Social logo

wasmedge / wasmedge_hyper_demo Goto Github PK

View Code? Open in Web Editor NEW
77.0 5.0 15.0 22.5 MB

Lightweight HTTP servers based on hyper / warp frameworks in the WasmEdge Runtime.

Rust 88.93% Dockerfile 11.07%
webassembly rust wasm http-server http http-client httpclient httpd

wasmedge_hyper_demo's Introduction

WasmEdge Hyper Demo

In this project, we demonstrate how to use hyper and tokio to build async http client in WebAssembly and execute it using WasmEdge.

Why tokio in WasmEdge?

There are growing demands to perform network requests in WASM and cloud computing. But it would be inefficient to perform network requests synchronously so we need async in WASM.

As tokio is widely accepted, we can bring many projects that depend on tokio to WASM if we can port tokio into WASM. After that, the developers can have async functions in WASM as well as efficient programs.

With the help of tokio support of WasmEdge, the developers can compile the projects that use tokio into WASM and execute it using WasmEdge.

Quickstart with Docker

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

Build all the examples using Docker. There is no need to install Rust or WasmEdge here since Docker sets it all up for you.

docker compose build

Then, you just need to type one command to run each example.

# To run the HTTP client example
$ docker compose run --no-TTY client
... ...

# To run the HTTP server
$ docker compose run --no-TTY -p 8080:8080 server
... ...
# Test the HTTP server using curl
$ curl http://localhost:8080/echo -X POST -d "WasmEdge"
WasmEdge

# To run the Warp HTTP server
$ docker compose run --no-TTY -p 8080:8080 server-warp
... ...
# Test the HTTP server using curl
$ curl http://localhost:8080/echo -X POST -d "WasmEdge"
WasmEdge

It runs both the client and server examples in this repo. See the Dockerfile and docker-compose.yml files. The client example will run and quit upon completion. The server example starts a server that listens for incoming HTTP requests, and you can interact with it through curl.

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

Prerequisites

We need install rust and wasm target first.

# install rust 
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env

# install wasm target 
rustup target add wasm32-wasi

Then install the WasmEdge. You will need all extensions to run the HTTP server with Tensorflow example.

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

Add dependencies in Cargo.toml

In this project, we add tokio and reqwest as dependencies.

[dependencies]
hyper_wasi = { version = "0.15", features = ["full"]}
tokio_wasi = { version = "1", features = ["rt", "macros", "net", "time", "io-util"]}

Examples

Details about the example apps are as below.

FAQ

use of unstable library feature 'wasi_ext'

If you are using rustc 1.64, you may encounter this error. There are two options:

  1. Update rustc to newer version. Validated versions are 1.65 and 1.59.
  2. Add #![feature(wasi_ext)] to the top of mio/src/lib.rs.

wasmedge_hyper_demo's People

Contributors

captainvincent avatar chris-crone avatar hydai avatar juntao avatar l-jasmine avatar mediosz avatar rumpl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

wasmedge_hyper_demo's Issues

100% CPU utilization even after request is served[ wasmedge_hyper_server]

Hi,

I am trying to run the docker image of wasmedge_hyper_server in knative. And I observed that even after I got a successful response, the CPU utilization of the pod is 100% until the pod is brought down after some idle time. I tried to replicate the issue with rust only code (without wasm) which appears to be working fine.
I was not able to pinpoint the issue. Can anyone please give some input on the same.

image

Best Regards
Abhishek Sharma

Build project in OSX

Hi,

I am trying to run the example this example:
https://github.com/jabrena/101-rust/blob/main/hello_world_wasm_backend/README.md

But I was not able to compile in OSX, I am receiving the following error:

docker compose build
[+] Building 1.3s (14/14) FINISHED                                                                                          
 => [internal] load build definition from Dockerfile                                                                   0.0s
 => => transferring dockerfile: 32B                                                                                    0.0s
 => [internal] load .dockerignore                                                                                      0.0s
 => => transferring context: 2B                                                                                        0.0s
 => [internal] load metadata for docker.io/library/rust:1.64                                                           1.2s
 => [buildbase 1/5] FROM docker.io/library/rust:1.64@sha256:922d814994d77f8e3ab8a7db45a277e9cebe41a557046eeef91a2e34b  0.0s
 => [internal] load build context                                                                                      0.0s
 => => transferring context: 86B                                                                                       0.0s
 => CACHED [buildbase 2/5] RUN rustup target add wasm32-wasi                                                           0.0s
 => CACHED [buildbase 3/5] WORKDIR /src                                                                                0.0s
 => CACHED [buildbase 4/5] COPY src ./src                                                                              0.0s
 => CACHED [buildbase 5/5] COPY Cargo.toml .                                                                           0.0s
 => CACHED [buildserver 1/3] COPY src ./src                                                                            0.0s
 => CACHED [buildserver 2/3] RUN pwd                                                                                   0.0s
 => CACHED [buildserver 3/3] RUN --mount=type=cache,target=/usr/local/cargo/git/db     --mount=type=cache,target=/usr  0.0s
 => CACHED [server 1/1] COPY --from=buildserver /src/target/wasm32-wasi/release/wasmedge_hyper_server.wasm wasmedge_h  0.0s
 => ERROR exporting to image                                                                                           0.0s
 => => exporting layers                                                                                                0.0s
 => => writing image sha256:9811b6619248d3846ca8c70d463c359854511b6539541b40dbfcb799b80e64f2                           0.0s
------
 > exporting to image:
------
failed to solve: operating system is not supported

docker compose up          
[+] Running 0/0
 ⠋ Container hello_world_wasm_backend-server-1  Creating                                                                                                                   0.0s
Error response from daemon: Unknown runtime specified io.containerd.wasmedge.v1

How to build the example in OSX?

Many thanks in advance

Juan Antonio

instantiation failed: unknown import when linking module: "rustls_client" , function name: "new_codec"

I was trying to follow this demo for a personal usecase and was running into an error. I then copied the HTTPs example line for line and I'm getting the following stack when running the WASM file:

[2023-09-06 15:23:47.397] [error] instantiation failed: unknown import, Code: 0x62
[2023-09-06 15:23:47.397] [error]     When linking module: "rustls_client" , function name: "new_codec"
[2023-09-06 15:23:47.397] [error]     At AST node: import description
[2023-09-06 15:23:47.397] [error]     At AST node: import section
[2023-09-06 15:23:47.397] [error]     At AST node: module

Not sure what the issue is here. I'm running this on a Github Codespace with the following system information:

uname -a
Linux codespaces-61c5b6 5.15.0-1041-azure #48-Ubuntu SMP Tue Jun 20 20:34:08 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Problems running on OSX (12.6)

Trying to run an example on my machine but I'm running into some weird issues, wondering if there's an easy explanation here

$ cargo --version                                                                                                                                                                                           
cargo 1.65.0 (4bc8f24d3 2022-10-20)
$ cargo build --target wasm32-wasi && wasmedge target/wasm32-wasi/debug/lsat-proxy.wasm                                                                                                                     
    Finished dev [unoptimized + debuginfo] target(s) in 0.07s
2022-11-22T15:32:18.202591Z  INFO lsat_proxy: Listening on http://0.0.0.0:8080
// when trying to curl from the second console
Error: Os { code: 52, kind: Unsupported, message: "Function not implemented" }

Looking at the code it errors exactly here when attempting to accept the incoming connection

 let (stream, _) = listener.accept().await?;

Not sure where to dig from here.

Edit: side note: running on M1 mac

Problems running on win

The example of server-warp running on WIN,

 cargo --version
cargo 1.70.0-nightly (15d090969 2023-03-21)
wasmedge.exe .\target\wasm32-wasi\release\wasmedge_warp_server.wasm
thread 'main' panicked at 'error binding to 0.0.0.0:38080: error creating server listener: Function not implemented (os error 52)', C:\Users\xxx\.cargo\registry\src\mirrors.sjtug.sjtu.edu.cn-7a04d2510079875b\warp_wasi-0.3.3\src\server.rs:213:27
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[2023-03-25 22:03:24.514] [error] execution failed: unreachable, Code: 0x89
[2023-03-25 22:03:24.515] [error]     In instruction: unreachable (0x00) , Bytecode offset: 0x001113c4
[2023-03-25 22:03:24.515] [error]     When executing function name: "_start"

if set rustup stable ,build error

 rustup default stable
info: using existing install for 'stable-x86_64-pc-windows-msvc'
info: default toolchain set to 'stable-x86_64-pc-windows-msvc'

  stable-x86_64-pc-windows-msvc unchanged - rustc 1.68.0 (2c8cc3432 2023-03-06)

 rustup target add wasm32-wasi
info: component 'rust-std' for target 'wasm32-wasi' is up to date
 cargo build --target wasm32-wasi --release
   Compiling block-buffer v0.10.4
   Compiling crypto-common v0.1.6
   Compiling tokio-macros v1.8.2
   Compiling thiserror-impl v1.0.40
   Compiling errno v0.2.8
   Compiling hashbrown v0.12.3
   Compiling futures-channel v0.3.27
   Compiling mime v0.3.17
   Compiling url v2.3.1
   Compiling mime_guess v2.0.4
   Compiling serde v1.0.158
   Compiling unicase v2.6.0
   Compiling try-lock v0.2.4
   Compiling safemem v0.3.3
   Compiling ryu v1.0.13
   Compiling fastrand v1.9.0
error[E0554]: `#![feature]` may not be used on the stable release channel
  --> C:\Users\tab\.cargo\registry\src\mirrors.sjtug.sjtu.edu.cn-7a04d2510079875b\errno-0.2.8\src\lib.rs:20:33
   |
20 | #![cfg_attr(target_os = "wasi", feature(thread_local))]
   |                                 ^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0554`.
error: could not compile `errno` due to previous error
warning: build failed, waiting for other jobs to finish...

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.