Skip to content

Commit

Permalink
Add redis docker support
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Yuan <[email protected]>
  • Loading branch information
juntao committed Feb 10, 2023
1 parent 2bdeea8 commit e2e1a2a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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!"
Expand Down
14 changes: 14 additions & 0 deletions redis/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
17 changes: 17 additions & 0 deletions redis/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion redis/src/main.rs
Original file line number Diff line number Diff line change
@@ -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());
Expand Down

0 comments on commit e2e1a2a

Please sign in to comment.