MySafeCode commited on
Commit
9bc2904
·
verified ·
1 Parent(s): 0e05d21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -64
app.py CHANGED
@@ -1,66 +1,69 @@
1
- from fastapi import FastAPI
2
  from pydantic import BaseModel
3
- from datetime import datetime
4
- from fastapi.openapi.docs import get_swagger_ui_html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
-
7
- app = FastAPI(title="1HIT RTC Signaling API")
8
-
9
- # ---------- Data Models ----------
10
- class Offer(BaseModel):
11
- offer: str
12
-
13
- class Answer(BaseModel):
14
- answer: str
15
-
16
- class IceCandidate(BaseModel):
17
- candidate: str
18
-
19
-
20
- # ---------- Basic Endpoints ----------
21
-
22
- # ---------- 4️⃣ Explicit Swagger docs endpoint ----------
23
- @app.get("/docs", include_in_schema=False)
24
- async def custom_swagger_ui_html():
25
- return get_swagger_ui_html(
26
- openapi_url=app.openapi_url,
27
- title=app.title + " - Swagger UI",
28
- oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
29
- swagger_js_url="https://unpkg.com/swagger-ui-dist@5.9.0/swagger-ui-bundle.js",
30
- swagger_css_url="https://unpkg.com/swagger-ui-dist@5.9.0/swagger-ui.css",
31
- )
32
-
33
- @app.get("/")
34
- def root():
35
- return {"message": "Hello HF!"}
36
-
37
- @app.get("/status")
38
- def status():
39
- return {"status": "running", "time": datetime.utcnow()}
40
-
41
-
42
- @app.get("/time")
43
- def get_time():
44
- return {"time": datetime.utcnow().strftime("%H:%M:%S UTC")}
45
-
46
-
47
- @app.post("/echo")
48
- def echo_message(payload: dict):
49
- return {"you_sent": payload}
50
-
51
-
52
- # ---------- RTC Signaling Endpoints ----------
53
- @app.post("/rtc/offer")
54
- def rtc_offer(data: Offer):
55
- # In production: store, forward, or route the offer to another client
56
- return {"received_offer": data.offer}
57
-
58
-
59
- @app.post("/rtc/answer")
60
- def rtc_answer(data: Answer):
61
- return {"received_answer": data.answer}
62
-
63
-
64
- @app.post("/rtc/ice")
65
- def rtc_ice(data: IceCandidate):
66
- return {"received_candidate": data.candidate}
 
1
+ from fastapi import FastAPI, Request
2
  from pydantic import BaseModel
3
+ from collections import deque
4
+ from datetime import datetime, timedelta
5
+ from typing import Optional
6
+ import uuid
7
+
8
+ app = FastAPI(title="Chat Handshake API")
9
+
10
+ # ---------- Configuration ----------
11
+ EXPIRATION_MINUTES = 5
12
+ MAX_MESSAGES = 1000
13
+ LOG_FILE = "activity.log"
14
+
15
+ # ---------- In-memory message storage ----------
16
+ messages = deque(maxlen=MAX_MESSAGES)
17
+
18
+ # ---------- Pydantic model ----------
19
+ class Message(BaseModel):
20
+ type: str # "offer", "answer", "chat", "connect"
21
+ from_user: str
22
+ to_user: Optional[str] = None
23
+ content: Optional[str] = None
24
+
25
+ # ---------- Helper functions ----------
26
+ def expire_messages():
27
+ cutoff = datetime.utcnow() - timedelta(minutes=EXPIRATION_MINUTES)
28
+ while messages and messages[0]["timestamp"] < cutoff:
29
+ messages.popleft()
30
+
31
+ def log_message(msg: dict, ip: str):
32
+ with open(LOG_FILE, "a") as f:
33
+ f.write(f"{datetime.utcnow()} {ip} {msg}\n")
34
+
35
+ # ---------- API Endpoints ----------
36
+
37
+ @app.post("/write")
38
+ async def write_message(msg: Message, request: Request):
39
+ expire_messages()
40
+ message_id = str(uuid.uuid4())
41
+ entry = {
42
+ "id": message_id,
43
+ "type": msg.type,
44
+ "from": msg.from_user,
45
+ "to": msg.to_user,
46
+ "content": msg.content,
47
+ "timestamp": datetime.utcnow()
48
+ }
49
+ messages.append(entry)
50
+ log_message(entry, request.client.host)
51
+ return {"status": "ok", "id": message_id}
52
+
53
+ @app.get("/read")
54
+ async def read_messages(username: str):
55
+ expire_messages()
56
+ not_mine = [m for m in messages if m["from"] != username]
57
+ return {"messages": not_mine}
58
+
59
+ @app.get("/readall")
60
+ async def read_all_messages():
61
+ expire_messages()
62
+ return {"messages": list(messages)}
63
+
64
+ @app.post("/retract")
65
+ async def retract_message(message_id: str):
66
+ global messages
67
+ messages = deque([m for m in messages if m["id"] != message_id], maxlen=MAX_MESSAGES)
68
+ return {"status": "ok"}
69