diff --git a/Cargo.lock b/Cargo.lock index 7ff2309..7e8abf7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -559,7 +559,26 @@ dependencies = [ ] [[package]] -name = "distributed-id-indexer" +name = "distributed-id-indexer-http" +version = "0.1.0" +dependencies = [ + "actix", + "actix-http", + "actix-web", + "dotenv", + "env_logger", + "lazy_static", + "listenfd", + "redis", + "serde", + "serde_json", + "tokio", + "tracing", + "tracing-actix-web", +] + +[[package]] +name = "distributed-id-indexer-pubsub" version = "0.1.0" dependencies = [ "actix", diff --git a/Cargo.toml b/Cargo.toml index d9e676d..fcea997 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" \ No newline at end of file +[workspace] +resolver = "2" +members = ["crates/http", "crates/pubsub"] diff --git a/README.md b/README.md index 54c61a1..1df1cc9 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/crates/http/.env.example b/crates/http/.env.example new file mode 100644 index 0000000..cd0178e --- /dev/null +++ b/crates/http/.env.example @@ -0,0 +1,4 @@ +REDIS_URL= +WEB_SERVER_PORT= +NUM_WORKERS= +LOG_LEVEL= \ No newline at end of file diff --git a/crates/http/Cargo.toml b/crates/http/Cargo.toml new file mode 100644 index 0000000..616c8f3 --- /dev/null +++ b/crates/http/Cargo.toml @@ -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" \ No newline at end of file diff --git a/crates/http/Dockerfile b/crates/http/Dockerfile new file mode 100644 index 0000000..7ae1e90 --- /dev/null +++ b/crates/http/Dockerfile @@ -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"] \ No newline at end of file diff --git a/crates/http/README.md b/crates/http/README.md new file mode 100644 index 0000000..3b23afd --- /dev/null +++ b/crates/http/README.md @@ -0,0 +1,3 @@ +### http - crate + +REST API Server for Retriving IDs from Redis \ No newline at end of file diff --git a/src/config/env.rs b/crates/http/src/config/env.rs similarity index 100% rename from src/config/env.rs rename to crates/http/src/config/env.rs diff --git a/src/config/mod.rs b/crates/http/src/config/mod.rs similarity index 100% rename from src/config/mod.rs rename to crates/http/src/config/mod.rs diff --git a/src/controller/data.rs b/crates/http/src/controller/data.rs similarity index 100% rename from src/controller/data.rs rename to crates/http/src/controller/data.rs diff --git a/src/controller/mod.rs b/crates/http/src/controller/mod.rs similarity index 100% rename from src/controller/mod.rs rename to crates/http/src/controller/mod.rs diff --git a/src/controller/root.rs b/crates/http/src/controller/root.rs similarity index 100% rename from src/controller/root.rs rename to crates/http/src/controller/root.rs diff --git a/src/libs/http.rs b/crates/http/src/libs/http.rs similarity index 100% rename from src/libs/http.rs rename to crates/http/src/libs/http.rs diff --git a/src/libs/mod.rs b/crates/http/src/libs/mod.rs similarity index 100% rename from src/libs/mod.rs rename to crates/http/src/libs/mod.rs diff --git a/crates/http/src/libs/redis.rs b/crates/http/src/libs/redis.rs new file mode 100644 index 0000000..e015552 --- /dev/null +++ b/crates/http/src/libs/redis.rs @@ -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(), + } +} diff --git a/src/main.rs b/crates/http/src/main.rs similarity index 64% rename from src/main.rs rename to crates/http/src/main.rs index c7dd958..721df36 100644 --- a/src/main.rs +++ b/crates/http/src/main.rs @@ -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(); diff --git a/crates/pubsub/.env.example b/crates/pubsub/.env.example new file mode 100644 index 0000000..c705770 --- /dev/null +++ b/crates/pubsub/.env.example @@ -0,0 +1 @@ +REDIS_URL= \ No newline at end of file diff --git a/crates/pubsub/Cargo.toml b/crates/pubsub/Cargo.toml new file mode 100644 index 0000000..075e132 --- /dev/null +++ b/crates/pubsub/Cargo.toml @@ -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" \ No newline at end of file diff --git a/crates/pubsub/Dockerfile b/crates/pubsub/Dockerfile new file mode 100644 index 0000000..5e86a7c --- /dev/null +++ b/crates/pubsub/Dockerfile @@ -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"] \ No newline at end of file diff --git a/crates/pubsub/README.md b/crates/pubsub/README.md new file mode 100644 index 0000000..6d36a2b --- /dev/null +++ b/crates/pubsub/README.md @@ -0,0 +1,3 @@ +### pubsub - crate + +Redis Pub/Sub Server which saves IDs to Redis \ No newline at end of file diff --git a/crates/pubsub/src/config/env.rs b/crates/pubsub/src/config/env.rs new file mode 100644 index 0000000..23827d2 --- /dev/null +++ b/crates/pubsub/src/config/env.rs @@ -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() +} \ No newline at end of file diff --git a/crates/pubsub/src/config/mod.rs b/crates/pubsub/src/config/mod.rs new file mode 100644 index 0000000..dea962f --- /dev/null +++ b/crates/pubsub/src/config/mod.rs @@ -0,0 +1 @@ +pub mod env; \ No newline at end of file diff --git a/crates/pubsub/src/libs/mod.rs b/crates/pubsub/src/libs/mod.rs new file mode 100644 index 0000000..ac3a5c6 --- /dev/null +++ b/crates/pubsub/src/libs/mod.rs @@ -0,0 +1 @@ +pub mod redis; \ No newline at end of file diff --git a/src/libs/redis.rs b/crates/pubsub/src/libs/redis.rs similarity index 94% rename from src/libs/redis.rs rename to crates/pubsub/src/libs/redis.rs index 21f6cc4..30e1c3f 100644 --- a/src/libs/redis.rs +++ b/crates/pubsub/src/libs/redis.rs @@ -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 } @@ -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 { diff --git a/crates/pubsub/src/main.rs b/crates/pubsub/src/main.rs new file mode 100644 index 0000000..28e129f --- /dev/null +++ b/crates/pubsub/src/main.rs @@ -0,0 +1,9 @@ +mod config; +mod libs; + +#[tokio::main] +async fn main() { + let _ = libs::redis::start_pub_sub().await; + + println!("PubSub started"); +} diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..70b0b7b --- /dev/null +++ b/docker-compose.yaml @@ -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: + + \ No newline at end of file