Skip to content

Commit

Permalink
feat: add dockerization for http client eg
Browse files Browse the repository at this point in the history
  • Loading branch information
sacha-l committed Dec 14, 2024
1 parent 5cca011 commit 2f6ceea
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/http_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
rust-ini = "0.20.0"
swarm_nl = { path = "../swarm_nl", features = ["tokio-runtime"] }
swarm_nl = { git = "https://github.com/algorealmInc/SwarmNL.git", features = ["tokio-runtime"] }
tokio = { version = "1.37.0", features = ["full"] }
# async-std = { version = "1.12", features = ["attributes"] }
reqwest = { version = "0.12.9", features = ["json"] }
Expand Down
23 changes: 23 additions & 0 deletions examples/http_client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Use a Rust base image
FROM rust:latest

# Install tmux and other utilities
RUN apt-get update && apt-get install -y tmux bash && apt-get clean

# Set the working directory
WORKDIR /usr/src/swarmnl-http-client-demo

# Copy the Rust project into the container
COPY . .

# Prebuild dependencies to speed up the runtime builds
RUN cargo build --release --features=first-node && \
cargo build --release --features=second-node && \
cargo build --release --features=third-node

# Copy the tmux startup script
COPY run_nodes.sh /run_nodes.sh
RUN chmod +x /run_nodes.sh

# Start tmux with the script
CMD ["/run_nodes.sh"]
52 changes: 52 additions & 0 deletions examples/http_client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# HTTP client example

SwarmNL supports seamless integration with HTTP clients working in the application layer by providing interfaces to handle network events as they occur. This example demonstrates how to send an HTTP POST request to a remote server in response to incoming replication data events.

To run this example, cd into the root of this directory and in separate terminals launch the following commands:

```bash
cargo run --features=first-node
```

Then the second node:

```bash
cargo run --features=second-node
```

And the third node:

```bash
cargo run --features=first-node
```

Replicate data across the network:

```bash
# in the first terminal
repl wonderful

# in the second terminal
repl great

# in the third terminal
repl amazing
```

You should see the message `Successfully sent POST request.` printed to the terminal.

## Run with Docker

Build:

```bash
docker build -t http-client .
```

Run:

```bash
docker run -it http-client
```

The Docker image will open three terminals and the script will submit commands to each one to demonstrate the example. To quit, use the `exit` command in each terminal.
33 changes: 33 additions & 0 deletions examples/http_client/run_nodes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# Start tmux session
tmux new-session -d -s rust-nodes "cargo run --features=first-node"
tmux split-window -h "cargo run --features=second-node"
tmux split-window -v "cargo run --features=third-node"

# Arrange panes for better layout
tmux select-layout tiled

# Give the nodes some time to start
sleep 10

# Send commands to each pane
# Pane 0 (first node)
tmux send-keys -t rust-nodes:0.0 "repl wonderful" C-m
sleep 3

# Pane 1 (second node)
tmux send-keys -t rust-nodes:0.1 "repl great" C-m
sleep 3

# Pane 2 (third node)
tmux send-keys -t rust-nodes:0.2 "repl amazing" C-m
sleep 3

# Attach to the session so you can observe the output
tmux attach-session -t rust-nodes

# Exit the sessions
tmux send-keys -t rust-nodes:0.0 "exit" C-m
tmux send-keys -t rust-nodes:0.1 "exit" C-m
tmux send-keys -t rust-nodes:0.2 "exit" C-m
2 changes: 2 additions & 0 deletions examples/http_client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//! providing interfaces to handle network events as they occur. This example demonstrates how to
//! send an HTTP POST request to a remote server in response to incoming replication data events.
#![allow(unused_variables, dead_code, unused_imports)]

use std::{
collections::HashMap,
io::{self, Write},
Expand Down

0 comments on commit 2f6ceea

Please sign in to comment.