2025-08-01 18:46:38 +02:00
|
|
|
from flask import Flask, Response, jsonify, request
|
2025-08-01 18:07:11 +02:00
|
|
|
from flask_cors import CORS
|
2025-08-01 18:46:38 +02:00
|
|
|
from room import Room
|
2025-08-01 18:23:27 +02:00
|
|
|
|
2025-08-01 18:07:11 +02:00
|
|
|
app = Flask(__name__)
|
|
|
|
CORS(app)
|
|
|
|
|
|
|
|
|
2025-08-01 18:23:27 +02:00
|
|
|
ROOMS: dict[int, Room] = {} # { room_id: room, ... }
|
|
|
|
|
|
|
|
|
|
|
|
def error(msg: str, status: int = 400) -> Response:
|
|
|
|
res = jsonify({"success": False, "error": msg})
|
|
|
|
res.status_code = status
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2025-08-01 18:07:11 +02:00
|
|
|
@app.route("/api")
|
|
|
|
def index():
|
|
|
|
return "hello from flask"
|
|
|
|
|
|
|
|
|
2025-08-01 18:23:27 +02:00
|
|
|
@app.route("/api/join")
|
|
|
|
def join():
|
|
|
|
room_id = request.args.get("room")
|
|
|
|
code = request.args.get("code")
|
|
|
|
|
|
|
|
if room_id is None:
|
|
|
|
return error("Missing room id")
|
|
|
|
|
|
|
|
if (room := ROOMS.get(int(room_id))) is None:
|
|
|
|
return error("Invalid room")
|
|
|
|
|
|
|
|
if room.pin is not None and room.pin != code:
|
|
|
|
return error("Invalid code")
|
|
|
|
|
|
|
|
return {"success": True, "ws": f"/ws/{room_id}"}
|
|
|
|
|
|
|
|
|
2025-08-01 18:07:11 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=True)
|