-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
150 lines (119 loc) · 4.55 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import queue
import random
import string
from fastapi import FastAPI, HTTPException
from model import Reaction
from fastapi.middleware.cors import CORSMiddleware
import firestore_handler
from datetime import datetime
# Config
APP_LINK = "http://localhost:8000"
# Initialize an empty dictionary to hold sessions
sessions = {}
app = FastAPI()
origins = [
"http://localhost",
"http://localhost:8000",
"http://127.0.0.1:8000/",
"https://haptic-excel.onrender.com/"
]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def get_current_reaction(session_id):
current_index = sessions[session_id]["index"]
sessions[session_id]["index"] = len(sessions[session_id]["reactions"])
return sessions[session_id]['reactions'][current_index:]
def generate_session_id(length: int = 6):
"""
Generate a unique session ID of uppercase letters and digits.
Parameters:
length (int): Desired length of the ID. Default is 6.
Returns:
str: Unique session ID.
Ensures uniqueness by checking against existing IDs in the global
`sessions` dictionary.
"""
# Generate a random string of upper case letters and digits
session_id = ''.join(random.choices(
string.ascii_uppercase + string.digits, k=length))
while sessions.get(session_id):
session_id = ''.join(random.choices(
string.ascii_uppercase + string.digits, k=length))
return session_id
@app.get("/get-session")
async def get_session():
session_id = generate_session_id()
link = f"{APP_LINK}/{session_id}"
# Initialize a queue for the session
sessions[session_id] = {'index': 0, 'reactions': [], 'active': True}
print(f'Started new session {session_id}')
return {"session_id": session_id, "link": link}
@app.post("/add-reaction/")
async def add_reaction(reaction_data: Reaction):
try:
session_id = reaction_data.sessionId
# Add current timestamp to the reaction data
reaction_data.timeStamp = datetime.utcnow()
if not sessions.get(session_id):
return "session not found"
elif not sessions.get(session_id)["active"]:
return "session ended"
sessions[session_id]['reactions'].append(reaction_data)
print(f"Add reaction:{reaction_data.reaction}")
return "Reaction added successfully"
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/end-session/{session_id}")
async def end_session(session_id: str):
if session_id not in sessions:
print('Session {session_id} not found')
raise HTTPException(
status_code=404, detail=f'Session {session_id} not found')
sessions[session_id]["active"] = False
try:
firestore_handler.add_data(sessions[session_id]["reactions"])
print('added data to firebase')
return "Session ended, added data to firebase"
except:
print('no data to add')
return "Session ended, no data to add"
@app.get("/get-reaction/{session_id}", response_model=list[Reaction])
async def get_reaction(session_id: str):
if session_id in sessions and sessions[session_id]['active']:
reactions = get_current_reaction(session_id)
if not reactions:
print(f'No new reaction to retrieve {session_id}')
return []
print(f'Successfully retrieved reactions for session {session_id}')
print(reactions)
return reactions
raise HTTPException(
status_code=404, detail=f'Reactions not found for session {session_id}')
@app.get("/get-session-data/{session_id}")
async def get_session_data(session_id: str):
try:
# Retrieve session data
session_data = sessions.get(session_id)
# Handle case where session data does not exist
if not session_data:
raise HTTPException(status_code=404, detail="Session not found")
# Return session data
return {"status": "success", "data": session_data}
except Exception as e:
# Handle unexpected errors
raise HTTPException(status_code=500, detail=str(e))
@app.get("/get-firebase-data/{session_id}")
async def get_firebase_data(session_id: str):
try:
# Retrieve firebase session data
firebase_data = firestore_handler.get_data(sessionID=session_id)
# Return session data
return {"status": "success", "data": firebase_data}
except Exception as e:
# Handle unexpected errors
raise HTTPException(status_code=500, detail=str(e))