From c84a835f55db0c17ce1c7b9f411a3ca1937ffdce Mon Sep 17 00:00:00 2001 From: Jakub Date: Wed, 16 Jul 2025 09:59:56 +0000 Subject: [PATCH] first commit --- backend/Dockerfile | 7 +++++++ backend/main.py | 33 +++++++++++++++++++++++++++++++++ backend/requirements.txt | 6 ++++++ docker-compose.yml | 29 +++++++++++++++++++++++++++++ worker/Dockerfile | 7 +++++++ worker/requirements.txt | 3 +++ worker/worker.py | 24 ++++++++++++++++++++++++ 7 files changed, 109 insertions(+) create mode 100644 backend/Dockerfile create mode 100644 backend/main.py create mode 100644 backend/requirements.txt create mode 100644 docker-compose.yml create mode 100644 worker/Dockerfile create mode 100644 worker/requirements.txt create mode 100644 worker/worker.py diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..d7e8bcc --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.11-slim +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "6000"] + diff --git a/backend/main.py b/backend/main.py new file mode 100644 index 0000000..5e4fa82 --- /dev/null +++ b/backend/main.py @@ -0,0 +1,33 @@ +from fastapi import FastAPI +from pydantic import BaseModel +from redis import Redis +from datetime import datetime +from pymongo import MongoClient +import json + +app = FastAPI() + +redis_client = Redis(host="redis", port=6379, decode_responses=True) +mongo_client = MongoClient("mongodb://mongo:27017") +mongo_db = mongo_client["logs"] +mongo_col = mongo_db["actions"] + +class Action(BaseModel): + user: str + type: str + +@app.post("/action") +async def log_action(action: Action): + entry = { + "user": action.user, + "type": action.type, + "timestamp": datetime.utcnow().isoformat() + } + redis_client.rpush("user_actions", json.dumps(entry)) + return {"status": "saved to redis"} + +@app.get("/actions") +async def get_actions(): + actions = list(mongo_col.find({}, {"_id": 0})) + return actions + diff --git a/backend/requirements.txt b/backend/requirements.txt new file mode 100644 index 0000000..c58c361 --- /dev/null +++ b/backend/requirements.txt @@ -0,0 +1,6 @@ +fastapi +uvicorn +redis +pydantic +pymongo + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..abe1d8c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +version: '3.8' + +services: + mongo: + image: mongo + ports: + - "27017:27017" + + redis: + image: redis + ports: + - "6379:6379" + + backend: + build: ./backend + ports: + - "6000:6000" + depends_on: + - redis + - mongo + + worker: + build: ./worker + depends_on: + - redis + - mongo + restart: always + entrypoint: ["sh", "-c", "while true; do python worker.py; sleep 60; done"] + diff --git a/worker/Dockerfile b/worker/Dockerfile new file mode 100644 index 0000000..1dead20 --- /dev/null +++ b/worker/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.11-slim +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +CMD ["python", "worker.py"] + diff --git a/worker/requirements.txt b/worker/requirements.txt new file mode 100644 index 0000000..e171922 --- /dev/null +++ b/worker/requirements.txt @@ -0,0 +1,3 @@ +pymongo +redis + diff --git a/worker/worker.py b/worker/worker.py new file mode 100644 index 0000000..9ca6878 --- /dev/null +++ b/worker/worker.py @@ -0,0 +1,24 @@ +from pymongo import MongoClient +from redis import Redis +import json + +redis_client = Redis(host='redis', port=6379, decode_responses=True) +mongo_client = MongoClient("mongodb://mongo:27017") +mongo_db = mongo_client["logs"] +mongo_col = mongo_db["actions"] + +def transfer_actions(): + actions = [] + while True: + entry = redis_client.lpop("user_actions") + if entry is None: + break + actions.append(json.loads(entry)) + + if actions: + mongo_col.insert_many(actions) + print(f"Transferred {len(actions)} actions to MongoDB") + +if __name__ == "__main__": + transfer_actions() +