Skip to content

Commit 53b84d3

Browse files
authored
Merge pull request #107 from wrongerror/main
Add WasmEdge sample
2 parents 26b2d2a + a7a3e3b commit 53b84d3

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[build]
2+
target="wasm32-wasi"
3+
4+
[target.wasm32-wasi]
5+
runner = "wasmedge"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM --platform=$BUILDPLATFORM rust:1.64 AS buildbase
2+
RUN rustup target add wasm32-wasi
3+
WORKDIR /src
4+
5+
FROM --platform=$BUILDPLATFORM buildbase AS buildserver
6+
COPY server /src
7+
RUN --mount=type=cache,target=/usr/local/cargo/git/db \
8+
--mount=type=cache,target=/usr/local/cargo/registry/cache \
9+
--mount=type=cache,target=/usr/local/cargo/registry/index \
10+
cargo build --target wasm32-wasi --release
11+
12+
FROM scratch AS server
13+
ENTRYPOINT [ "wasmedge_hyper_server.wasm" ]
14+
COPY --from=buildserver /src/target/wasm32-wasi/release/wasmedge_hyper_server.wasm wasmedge_hyper_server.wasm
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Sample Function WasmEdge ( written in Rust )
2+
3+
## Prerequisites
4+
5+
### OpenFunction
6+
7+
You can refer to the [Installation Guide](https://github.com/OpenFunction/OpenFunction#install-openfunction) to setup OpenFunction.
8+
9+
## Run it locally
10+
11+
1. Build
12+
13+
```bash
14+
cargo build --target wasm32-wasi --release
15+
```
16+
17+
2. Run
18+
19+
```bash
20+
wasmedge target/wasm32-wasi/release/wasmedge_hyper_server.wasm
21+
```
22+
23+
3. Test
24+
25+
Run the following from another terminal.
26+
27+
```bash
28+
$ curl http://localhost:8080/echo -X POST -d "WasmEdge"
29+
WasmEdge
30+
```
31+
32+
## Deployment
33+
34+
> To setup `WasmEdge` workload runtime in kubernetes cluster and push images to a container registry,
35+
> please refer to the [prerequisites](../../getting-started/Quickstarts/prerequisites) section for more info.
36+
37+
1. Create function
38+
39+
```shell
40+
kubectl apply -f wasmedge-http-server.yaml
41+
```
42+
43+
2. Check the function status
44+
45+
```shell
46+
kubectl get functions.core.openfunction.io -w
47+
NAME BUILDSTATE SERVINGSTATE BUILDER SERVING ADDRESS AGE
48+
wasmedge-http-server Succeeded Running builder-4p2qq serving-lrd8c http://wasmedge-http-server.default.svc.cluster.local/echo 12m
49+
```
50+
51+
3. Access function
52+
53+
Once the `BUILDSTATE` becomes `Succeeded` and the `SERVINGSTATE` becomes `Running`, you can access this function through the address in the `ADDRESS` field:
54+
You can observe the process of a function with the following command:
55+
56+
```shell
57+
kubectl run curl --image=radial/busyboxplus:curl -i --tty
58+
curl http://wasmedge-http-server.default.svc.cluster.local/echo -X POST -d "WasmEdge"
59+
WasmEdge
60+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "wasmedge_hyper_server"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
hyper_wasi = { version = "0.15", features = ["full"]}
8+
tokio_wasi = { version = "1.21", features = ["rt", "macros", "net", "time", "io-util"]}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::net::SocketAddr;
2+
3+
use hyper::server::conn::Http;
4+
use hyper::service::service_fn;
5+
use hyper::{Body, Method, Request, Response, StatusCode};
6+
use tokio::net::TcpListener;
7+
8+
/// This is our service handler. It receives a Request, routes on its
9+
/// path, and returns a Future of a Response.
10+
async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
11+
match (req.method(), req.uri().path()) {
12+
// Serve some instructions at /
13+
(&Method::GET, "/") => Ok(Response::new(Body::from(
14+
"Try POSTing data to /echo such as: `curl localhost:8080/echo -XPOST -d 'hello world'`",
15+
))),
16+
17+
// Simply echo the body back to the client.
18+
(&Method::POST, "/echo") => Ok(Response::new(req.into_body())),
19+
20+
(&Method::POST, "/echo/reversed") => {
21+
let whole_body = hyper::body::to_bytes(req.into_body()).await?;
22+
23+
let reversed_body = whole_body.iter().rev().cloned().collect::<Vec<u8>>();
24+
Ok(Response::new(Body::from(reversed_body)))
25+
}
26+
27+
// Return the 404 Not Found for other routes.
28+
_ => {
29+
let mut not_found = Response::default();
30+
*not_found.status_mut() = StatusCode::NOT_FOUND;
31+
Ok(not_found)
32+
}
33+
}
34+
}
35+
36+
#[tokio::main(flavor = "current_thread")]
37+
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
38+
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
39+
40+
let listener = TcpListener::bind(addr).await?;
41+
println!("Listening on http://{}", addr);
42+
loop {
43+
let (stream, _) = listener.accept().await?;
44+
45+
tokio::task::spawn(async move {
46+
if let Err(err) = Http::new().serve_connection(stream, service_fn(echo)).await {
47+
println!("Error serving connection: {:?}", err);
48+
}
49+
});
50+
}
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
apiVersion: core.openfunction.io/v1beta1
2+
kind: Function
3+
metadata:
4+
name: wasmedge-http-server
5+
spec:
6+
workloadRuntime: wasmedge
7+
image: openfunctiondev/wasmedge_http_server:0.1.0
8+
imageCredentials:
9+
name: push-secret
10+
build:
11+
dockerfile: Dockerfile
12+
srcRepo:
13+
revision: main
14+
sourceSubPath: functions/knative/wasmedge/http-server
15+
url: https://github.com/OpenFunction/samples
16+
port: 8080
17+
route:
18+
rules:
19+
- matches:
20+
- path:
21+
type: PathPrefix
22+
value: /echo
23+
serving:
24+
runtime: knative
25+
scaleOptions:
26+
minReplicas: 0
27+
template:
28+
containers:
29+
- command:
30+
- /wasmedge_hyper_server.wasm
31+
imagePullPolicy: IfNotPresent
32+
livenessProbe:
33+
initialDelaySeconds: 3
34+
periodSeconds: 30
35+
tcpSocket:
36+
port: 8080
37+
name: function

0 commit comments

Comments
 (0)