diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index ff84d25..0054af8 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -59,8 +59,8 @@ jobs: # sudo pg_ctlcluster 12 main start sudo systemctl start postgresql.service sleep 10 - sudo createuser -w wasmedge - sudo createdb -O wasmedge testdb + sudo -u postgres createuser -w wasmedge + sudo -u postgres createdb -O wasmedge testdb - name: Install Anna KVS and run router run: | @@ -139,7 +139,7 @@ jobs: cd redis cargo build --target wasm32-wasi --release wasmedgec target/wasm32-wasi/release/wasmedge-redis-client-examples.wasm wasmedge-redis-client-examples.wasm - resp=$(wasmedge wasmedge-redis-client-examples.wasm 2>&1) + resp=$(wasmedge --env "REDIS_URL=redis://localhost/" wasmedge-redis-client-examples.wasm 2>&1) echo "$resp" if [[ $resp == *"UTC"* ]]; then echo -e "Execution Success!" diff --git a/redis/Dockerfile b/redis/Dockerfile new file mode 100644 index 0000000..e3122e5 --- /dev/null +++ b/redis/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 buildclient +COPY . /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 client +ENTRYPOINT [ "wasmedge-redis-client-examples.wasm" ] +COPY --link --from=buildclient /src/target/wasm32-wasi/release/wasmedge-redis-client-examples.wasm wasmedge-redis-client-examples.wasm diff --git a/redis/README.md b/redis/README.md index 145fc85..b846d13 100644 --- a/redis/README.md +++ b/redis/README.md @@ -4,5 +4,5 @@ ```bash cargo build --target wasm32-wasi --release -wasmedge target/wasm32-wasi/release/wasmedge-redis-client-examples.wasm +wasmedge --env "REDIS_URL=redis://localhost/" target/wasm32-wasi/release/wasmedge-redis-client-examples.wasm ``` diff --git a/redis/docker-compose.yml b/redis/docker-compose.yml new file mode 100644 index 0000000..2696df5 --- /dev/null +++ b/redis/docker-compose.yml @@ -0,0 +1,17 @@ +services: + client: + image: demo-redis + platform: wasi/wasm + build: + context: . + environment: + REDIS_URL: redis://cache/ + RUST_BACKTRACE: full + restart: on-failure + runtime: io.containerd.wasmedge.v1 + cache: + image: redis:6.2-alpine + restart: always + ports: + - '6379:6379' + command: redis-server --save 20 1 --loglevel warning diff --git a/redis/src/main.rs b/redis/src/main.rs index 43f32e3..ab364f9 100644 --- a/redis/src/main.rs +++ b/redis/src/main.rs @@ -1,10 +1,18 @@ use redis::Commands; use anyhow::Result; +fn get_url() -> String { + if let Ok(url) = std::env::var("REDIS_URL") { + url + } else { + "redis://127.0.0.1/".into() + } +} + #[tokio::main(flavor = "current_thread")] async fn main() -> Result<()> { // connect to redis - let client = redis::Client::open("redis://127.0.0.1/")?; + let client = redis::Client::open(&*get_url())?; let mut con = client.get_connection()?; let time = format!("{}", chrono::Utc::now());