Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added monorepos + Docker support #1

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 3 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
[package]
name = "distributed-id-indexer"
version = "0.1.0"
edition = "2021"

[dependencies]
dotenv = "0.15.0"
redis = { version = "0.26.1", features = ["tokio-comp","cluster-async"] }
tokio = { version = "1.40.0", features = ["full"] }
actix-web = "4.3.1"
actix-http = "3.4.0"
serde = { version = "1.0.167", features = ["derive"] }
serde_json = "1.0.100"
actix = "0.13.0"
env_logger = "0.11.0"
lazy_static = "1.4"
listenfd = "1.0.1"
tracing-actix-web = "0.7"
tracing = "0.1"
[workspace]
resolver = "2"
members = ["crates/http", "crates/pubsub"]
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,22 @@ NUM_WORKERS=
LOG_LEVEL=
```

### 🎁 Crates

| Name | Description | Visit |
|------|-------------|-------|
| http | REST API Server for Retriving IDs from Redis | [Open](./crates/http/) |
| pubsub | Redis Pub/Sub Server which saves IDs to Redis | [Open](./crates/pubsub/) |

### 🚀 Usage

```bash
$ cargo run
$ cargo run --bin http
$ cargo run --bin pubsub

// or

$ docker-compose up
```

### 📝 License
4 changes: 4 additions & 0 deletions crates/http/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
REDIS_URL=
WEB_SERVER_PORT=
NUM_WORKERS=
LOG_LEVEL=
19 changes: 19 additions & 0 deletions crates/http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "distributed-id-indexer-http"
version = "0.1.0"
edition = "2021"

[dependencies]
dotenv = "0.15.0"
redis = { version = "0.26.1", features = ["tokio-comp","cluster-async"] }
tokio = { version = "1.40.0", features = ["full"] }
actix-web = "4.3.1"
actix-http = "3.4.0"
serde = { version = "1.0.167", features = ["derive"] }
serde_json = "1.0.100"
actix = "0.13.0"
env_logger = "0.11.0"
lazy_static = "1.4"
listenfd = "1.0.1"
tracing-actix-web = "0.7"
tracing = "0.1"
14 changes: 14 additions & 0 deletions crates/http/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM rust:1.80.1-slim-bullseye

# View app name in Cargo.toml
ARG APP_NAME=distributed-id-indexer-http

WORKDIR /app

COPY . .
RUN cargo build --release
RUN cp ./target/release/$APP_NAME /distributed-id-indexer-http

EXPOSE 8000

CMD ["/distributed-id-indexer-http"]
3 changes: 3 additions & 0 deletions crates/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### http - crate

REST API Server for Retriving IDs from Redis
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions crates/http/src/libs/redis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use redis::AsyncCommands;

pub async fn connection_to_redis(
redis_url: &str,
) -> redis::Client {
let client: redis::Client = redis::Client::open(redis_url).unwrap();
client
}

pub async fn get_value(connection: &mut redis::aio::MultiplexedConnection, key: &str) -> String {
let value = connection.get(key).await;

match value {
Ok(value) => value,
Err(_) => "".to_string(),
}
}
6 changes: 0 additions & 6 deletions src/main.rs → crates/http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ mod controller;

#[tokio::main]
async fn main() {
tokio::task::spawn_blocking(|| {
let _ = libs::redis::start_pub_sub();

println!("PubSub started");
});

tokio::task::spawn_blocking(|| {
let _ = libs::http::start_web_server();

Expand Down
1 change: 1 addition & 0 deletions crates/pubsub/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REDIS_URL=
19 changes: 19 additions & 0 deletions crates/pubsub/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "distributed-id-indexer-pubsub"
version = "0.1.0"
edition = "2021"

[dependencies]
dotenv = "0.15.0"
redis = { version = "0.26.1", features = ["tokio-comp","cluster-async"] }
tokio = { version = "1.40.0", features = ["full"] }
actix-web = "4.3.1"
actix-http = "3.4.0"
serde = { version = "1.0.167", features = ["derive"] }
serde_json = "1.0.100"
actix = "0.13.0"
env_logger = "0.11.0"
lazy_static = "1.4"
listenfd = "1.0.1"
tracing-actix-web = "0.7"
tracing = "0.1"
12 changes: 12 additions & 0 deletions crates/pubsub/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM rust:1.80.1-slim-bullseye

# View app name in Cargo.toml
ARG APP_NAME=distributed-id-indexer-pubsub

WORKDIR /app

COPY . .
RUN cargo build --release
RUN cp ./target/release/$APP_NAME /distributed-id-indexer-pubsub

CMD ["/distributed-id-indexer-pubsub"]
3 changes: 3 additions & 0 deletions crates/pubsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### pubsub - crate

Redis Pub/Sub Server which saves IDs to Redis
20 changes: 20 additions & 0 deletions crates/pubsub/src/config/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::env;
use dotenv::dotenv;

pub struct Env {
pub redis_url: String,
}

impl Env {
pub fn new() -> Self {
dotenv().ok();

Self {
redis_url: env::var("REDIS_URL").expect("REDIS_URL must be set"),
}
}
}

pub fn get_env() -> Env {
Env::new()
}
1 change: 1 addition & 0 deletions crates/pubsub/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod env;
1 change: 1 addition & 0 deletions crates/pubsub/src/libs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod redis;
4 changes: 2 additions & 2 deletions src/libs/redis.rs → crates/pubsub/src/libs/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use redis::AsyncCommands;
pub async fn connection_to_redis(
redis_url: &str,
) -> redis::Client {
// read the redis connection string from the environment
let client: redis::Client = redis::Client::open(redis_url).unwrap();
client
}
Expand Down Expand Up @@ -33,11 +32,12 @@ pub async fn start_pub_sub() {

pubsub.psubscribe("snowflake:id:set:*").unwrap();

println!("Subscribed to snowflake:id:set:*");

loop {
let msg = pubsub.get_message().unwrap();
let payload: String = msg.get_payload().unwrap();

// Payload is in the format of "a-z:a-z:0-9"
let payload_parts: Vec<&str> = payload.split(":").collect();

if payload_parts.len() != 3 {
Expand Down
9 changes: 9 additions & 0 deletions crates/pubsub/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod config;
mod libs;

#[tokio::main]
async fn main() {
let _ = libs::redis::start_pub_sub().await;

println!("PubSub started");
}
41 changes: 41 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: '3.7'

services:
http:
build:
context: ./crates/http
dockerfile: Dockerfile
ports:
- "8181:4000"
depends_on:
- redis
environment:
- REDIS_URL=redis://redis:6379
- WEB_SERVER_PORT=4000
- NUM_WORKERS=1
- LOG_LEVEL="info"
networks:
- dii

pubsub:
build:
context: ./crates/pubsub
dockerfile: Dockerfile
depends_on:
- redis
environment:
- REDIS_URL=redis://redis:6379
networks:
- dii

redis:
image: "redis:alpine"
networks:
- dii
ports:
- "6380:6379"

networks:
dii:


Loading