From 556fb8bcf870da2d7c818e116c74d411c423fe51 Mon Sep 17 00:00:00 2001 From: geezo Date: Mon, 15 Sep 2025 19:44:06 +0000 Subject: [PATCH] add files --- app.py | 204 +++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 20 +++++ dockerfile | 20 +++++ reddit.py | 1 + 4 files changed, 245 insertions(+) create mode 100644 app.py create mode 100644 docker-compose.yml create mode 100644 dockerfile create mode 100644 reddit.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..f19d448 --- /dev/null +++ b/app.py @@ -0,0 +1,204 @@ +from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request +from fastapi.responses import HTMLResponse +import asyncio, json +from typing import Set + +app = FastAPI() + +class ConnectionManager: + def __init__(self): + self.active: Set[WebSocket] = set() + self._lock = asyncio.Lock() + + async def connect(self, ws: WebSocket): + await ws.accept() + async with self._lock: + self.active.add(ws) + + async def disconnect(self, ws: WebSocket): + async with self._lock: + self.active.discard(ws) + + async def broadcast(self, message: str): + data = json.dumps({"text": message}) + async with self._lock: + dead = [] + for ws in self.active: + try: + await ws.send_text(data) + except Exception: + dead.append(ws) + for ws in dead: + self.active.discard(ws) + +manager = ConnectionManager() + +@app.get("/", response_class=HTMLResponse) +async def index(): + return """ + + + + + Messages + + + + + + +
+

Messages

+
+
+ + + +""" + +@app.post("/send") +async def send(request: Request): + text = "" + try: + payload = await request.json() + if isinstance(payload, dict): + text = payload.get("text", "") + except Exception: + try: + form = await request.form() + text = form.get("text", "") + except Exception: + pass + await manager.broadcast(text or "") + return {"message": text or ""} + +@app.websocket("/ws") +async def ws_endpoint(ws: WebSocket): + await manager.connect(ws) + try: + while True: + await ws.receive_text() + except WebSocketDisconnect: + await manager.disconnect(ws) + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b7a62b1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +version: "3.8" + +services: + n8n: + image: n8nio/n8n:latest + container_name: n8n + restart: always + ports: + - "5678:5678" + environment: + - GENERIC_TIMEZONE=America/New_York # adjust to your timezone + - N8N_BASIC_AUTH_ACTIVE=true + - N8N_BASIC_AUTH_USER=admin + - N8N_BASIC_AUTH_PASSWORD=admin123 + - N8N_SECURE_COOKIE=false + volumes: + - n8n_data:/home/node/.n8n + +volumes: + n8n_data: diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..ebc0452 --- /dev/null +++ b/dockerfile @@ -0,0 +1,20 @@ +# Minimal, production-ready image +FROM python:3.12-slim + +# Prevent Python from writing .pyc files; ensure unbuffered logs +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +WORKDIR /app + +# Install only what we need +RUN pip install --no-cache-dir fastapi "uvicorn[standard]" + +# Copy the application +COPY app.py /app/app.py + +# Expose the app port +EXPOSE 8000 + +# Start the server +CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/reddit.py b/reddit.py new file mode 100644 index 0000000..e49b151 --- /dev/null +++ b/reddit.py @@ -0,0 +1 @@ +import \ No newline at end of file