team-1/backend/src/app.py
Simone Tesini b06d02ca62 add tests
2025-08-01 22:10:59 +02:00

50 lines
971 B
Python

import dotenv
from flask import Flask, Response, jsonify, request
from flask_cors import CORS
from .room import Room, test_algo
from .song import init_db
dotenv.load_dotenv()
app = Flask(__name__)
CORS(app)
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
@app.route("/api")
def index():
return "hello from flask"
@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}"}
init_db()
test_algo()
exit()
if __name__ == "__main__":
app.run(debug=True)