ChandimaPrabath commited on
Commit
4e3a77a
1 Parent(s): 2007b93
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. app.py +54 -0
  3. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ COPY --chown=user . /app
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect
2
+ from pydantic import BaseModel
3
+ from typing import Dict
4
+ import json
5
+ from cryptography.hazmat.primitives.asymmetric import rsa, padding
6
+ from cryptography.hazmat.primitives import serialization, hashes
7
+
8
+ app = FastAPI()
9
+
10
+ # In-memory storage for public keys
11
+ public_keys: Dict[str, str] = {}
12
+ messages: Dict[str, list] = {}
13
+
14
+ class Message(BaseModel):
15
+ recipient: str
16
+ message: str
17
+
18
+ @app.post("/register")
19
+ async def register(username: str, public_key: str):
20
+ public_keys[username] = public_key
21
+ return {"status": "Public key registered successfully"}
22
+
23
+ @app.get("/public_key/{username}")
24
+ async def get_public_key(username: str):
25
+ if username in public_keys:
26
+ return {"public_key": public_keys[username]}
27
+ raise HTTPException(status_code=404, detail="User not found")
28
+
29
+ @app.post("/send_message")
30
+ async def send_message(message: Message):
31
+ if message.recipient not in messages:
32
+ messages[message.recipient] = []
33
+ messages[message.recipient].append(message.message)
34
+ return {"status": "Message sent"}
35
+
36
+ @app.websocket("/ws/{username}")
37
+ async def websocket_endpoint(websocket: WebSocket, username: str):
38
+ await websocket.accept()
39
+ while True:
40
+ try:
41
+ data = await websocket.receive_text()
42
+ message_data = json.loads(data)
43
+ recipient = message_data.get("recipient")
44
+ message = message_data.get("message")
45
+
46
+ if recipient in messages:
47
+ messages[recipient].append(message)
48
+ else:
49
+ messages[recipient] = [message]
50
+
51
+ # Send the new message to the recipient
52
+ await websocket.send_text(json.dumps({"recipient": recipient, "message": message}))
53
+ except WebSocketDisconnect:
54
+ break
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi
2
+ cryptography