diff --git a/functions/knative/wasmedge/http-server/.cargo/config.toml b/functions/knative/wasmedge/http-server/.cargo/config.toml new file mode 100644 index 0000000..50cdadf --- /dev/null +++ b/functions/knative/wasmedge/http-server/.cargo/config.toml @@ -0,0 +1,5 @@ +[build] +target="wasm32-wasi" + +[target.wasm32-wasi] +runner = "wasmedge" \ No newline at end of file diff --git a/functions/knative/wasmedge/http-server/Dockerfile b/functions/knative/wasmedge/http-server/Dockerfile new file mode 100644 index 0000000..0864789 --- /dev/null +++ b/functions/knative/wasmedge/http-server/Dockerfile @@ -0,0 +1,14 @@ +FROM --platform=$BUILDPLATFORM rust:1.64 AS buildbase +RUN rustup target add wasm32-wasi +WORKDIR /src + +FROM --platform=$BUILDPLATFORM buildbase AS buildserver +COPY server /src +RUN --mount=type=cache,target=/usr/local/cargo/git/db \ + --mount=type=cache,target=/usr/local/cargo/registry/cache \ + --mount=type=cache,target=/usr/local/cargo/registry/index \ + cargo build --target wasm32-wasi --release + +FROM scratch AS server +ENTRYPOINT [ "wasmedge_hyper_server.wasm" ] +COPY --from=buildserver /src/target/wasm32-wasi/release/wasmedge_hyper_server.wasm wasmedge_hyper_server.wasm diff --git a/functions/knative/wasmedge/http-server/README.md b/functions/knative/wasmedge/http-server/README.md new file mode 100644 index 0000000..6ffa1be --- /dev/null +++ b/functions/knative/wasmedge/http-server/README.md @@ -0,0 +1,60 @@ +# Sample Function WasmEdge ( written in Rust ) + +## Prerequisites + +### OpenFunction + +You can refer to the [Installation Guide](https://github.com/OpenFunction/OpenFunction#install-openfunction) to setup OpenFunction. + +## Run it locally + +1. Build + +```bash +cargo build --target wasm32-wasi --release +``` + +2. Run + +```bash +wasmedge target/wasm32-wasi/release/wasmedge_hyper_server.wasm +``` + +3. Test + +Run the following from another terminal. + +```bash +$ curl http://localhost:8080/echo -X POST -d "WasmEdge" +WasmEdge +``` + +## Deployment + +> To setup `WasmEdge` workload runtime in kubernetes cluster and push images to a container registry, +> please refer to the [prerequisites](../../getting-started/Quickstarts/prerequisites) section for more info. + +1. Create function + +```shell +kubectl apply -f wasmedge-http-server.yaml + ``` + +2. Check the function status + +```shell +kubectl get functions.core.openfunction.io -w +NAME BUILDSTATE SERVINGSTATE BUILDER SERVING ADDRESS AGE +wasmedge-http-server Succeeded Running builder-4p2qq serving-lrd8c http://wasmedge-http-server.default.svc.cluster.local/echo 12m + ``` + +3. Access function + +Once the `BUILDSTATE` becomes `Succeeded` and the `SERVINGSTATE` becomes `Running`, you can access this function through the address in the `ADDRESS` field: +You can observe the process of a function with the following command: + +```shell +kubectl run curl --image=radial/busyboxplus:curl -i --tty +curl http://wasmedge-http-server.default.svc.cluster.local/echo -X POST -d "WasmEdge" +WasmEdge +``` diff --git a/functions/knative/wasmedge/http-server/server/Cargo.toml b/functions/knative/wasmedge/http-server/server/Cargo.toml new file mode 100644 index 0000000..b525353 --- /dev/null +++ b/functions/knative/wasmedge/http-server/server/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "wasmedge_hyper_server" +version = "0.1.0" +edition = "2021" + +[dependencies] +hyper_wasi = { version = "0.15", features = ["full"]} +tokio_wasi = { version = "1.21", features = ["rt", "macros", "net", "time", "io-util"]} diff --git a/functions/knative/wasmedge/http-server/server/src/main.rs b/functions/knative/wasmedge/http-server/server/src/main.rs new file mode 100644 index 0000000..f3e5d48 --- /dev/null +++ b/functions/knative/wasmedge/http-server/server/src/main.rs @@ -0,0 +1,51 @@ +use std::net::SocketAddr; + +use hyper::server::conn::Http; +use hyper::service::service_fn; +use hyper::{Body, Method, Request, Response, StatusCode}; +use tokio::net::TcpListener; + +/// This is our service handler. It receives a Request, routes on its +/// path, and returns a Future of a Response. +async fn echo(req: Request
) -> Result