first commit
This commit is contained in:
7
backend/Dockerfile
Normal file
7
backend/Dockerfile
Normal file
@ -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"]
|
||||||
|
|
33
backend/main.py
Normal file
33
backend/main.py
Normal file
@ -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
|
||||||
|
|
6
backend/requirements.txt
Normal file
6
backend/requirements.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
fastapi
|
||||||
|
uvicorn
|
||||||
|
redis
|
||||||
|
pydantic
|
||||||
|
pymongo
|
||||||
|
|
29
docker-compose.yml
Normal file
29
docker-compose.yml
Normal file
@ -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"]
|
||||||
|
|
7
worker/Dockerfile
Normal file
7
worker/Dockerfile
Normal file
@ -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"]
|
||||||
|
|
3
worker/requirements.txt
Normal file
3
worker/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pymongo
|
||||||
|
redis
|
||||||
|
|
24
worker/worker.py
Normal file
24
worker/worker.py
Normal file
@ -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()
|
||||||
|
|
Reference in New Issue
Block a user