Add initial room classes & join API
This commit is contained in:
parent
2bd81f859e
commit
fcdc9cfaed
2 changed files with 38 additions and 1 deletions
|
@ -1,14 +1,42 @@
|
|||
from flask import Flask
|
||||
from flask import Flask, request, Response, jsonify
|
||||
from flask_cors import CORS
|
||||
|
||||
from .classes import Room
|
||||
|
||||
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}"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue