add global state
This commit is contained in:
parent
07964c469e
commit
e37aa36a3e
4 changed files with 30 additions and 20 deletions
|
@ -1,10 +1,12 @@
|
|||
import dotenv
|
||||
from connect import get_connection
|
||||
from flask import Flask, Response, jsonify, request
|
||||
from flask_cors import CORS
|
||||
from flask_socketio import SocketIO, emit
|
||||
from state import State
|
||||
|
||||
from .room import Room, test_algo
|
||||
from .song import Song, init_db
|
||||
from .room import Room
|
||||
from .song import init_db
|
||||
|
||||
# from .song_fetch import *
|
||||
|
||||
|
@ -16,8 +18,9 @@ app.config["SECRET_KEY"] = "your_secret_key"
|
|||
socketio = SocketIO(app)
|
||||
CORS(app)
|
||||
|
||||
|
||||
ROOMS: dict[int, Room] = {} # { room_id: room, ... }
|
||||
db_conn = get_connection()
|
||||
state = State(app, db_conn.cursor())
|
||||
init_db(state.db)
|
||||
|
||||
|
||||
def error(msg: str, status: int = 400) -> Response:
|
||||
|
@ -34,7 +37,7 @@ def join():
|
|||
if room_id is None:
|
||||
return error("Missing room id")
|
||||
|
||||
if (room := ROOMS.get(int(room_id))) is None:
|
||||
if (room := state.rooms.get(int(room_id))) is None:
|
||||
return error("Invalid room")
|
||||
|
||||
if room.pin is not None and room.pin != code:
|
||||
|
@ -48,7 +51,7 @@ def queue():
|
|||
if (room_id := request.args.get("room")) is None:
|
||||
return error("Missing room id")
|
||||
|
||||
if (room := ROOMS.get(int(room_id))) is None:
|
||||
if (room := state.rooms.get(int(room_id))) is None:
|
||||
return error("Invalid room")
|
||||
|
||||
return {"success": True, "queue": room.playing}
|
||||
|
@ -59,7 +62,7 @@ def queue_next():
|
|||
if (room_id := request.args.get("room")) is None:
|
||||
return error("Missing room id")
|
||||
|
||||
if (room := ROOMS.get(int(room_id))) is None:
|
||||
if (room := state.rooms.get(int(room_id))) is None:
|
||||
return error("Invalid room")
|
||||
|
||||
room.playing_idx += 1
|
||||
|
@ -91,7 +94,7 @@ def room_new():
|
|||
lat, lon = room_cords.split(",")
|
||||
|
||||
room = Room(
|
||||
id=max(ROOMS or [0]) + 1, #
|
||||
id=max(state.rooms or [0]) + 1, #
|
||||
coord=(float(lat), float(lon)),
|
||||
name=room_name,
|
||||
pin=room_pin,
|
||||
|
@ -102,7 +105,7 @@ def room_new():
|
|||
playing_idx=-1,
|
||||
)
|
||||
|
||||
ROOMS[room.id] = room
|
||||
state.rooms[room.id] = room
|
||||
|
||||
return {"success": True, "room_id": room.id}
|
||||
|
||||
|
@ -116,13 +119,9 @@ def room():
|
|||
"private": room.pin is not None,
|
||||
"coords": room.coord,
|
||||
}
|
||||
for room in ROOMS.values()
|
||||
for room in state.rooms.values()
|
||||
]
|
||||
|
||||
|
||||
init_db()
|
||||
test_algo()
|
||||
exit()
|
||||
|
||||
if __name__ == "__main__":
|
||||
socketio.run(app, debug=True)
|
||||
|
|
|
@ -4,4 +4,5 @@ import sqlite3
|
|||
def get_connection():
|
||||
conn = sqlite3.connect(".data/jukebox.db")
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.autocommit = True
|
||||
return conn
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
from dataclasses import dataclass
|
||||
from sqlite3 import Cursor
|
||||
|
||||
from .connect import get_connection
|
||||
|
||||
|
||||
def init_db():
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
def init_db(db: Cursor):
|
||||
db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS songs (
|
||||
mbid TEXT PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
|
@ -16,8 +15,6 @@ def init_db():
|
|||
youtube_id TEXT NOT NULL
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
13
backend/src/state.py
Normal file
13
backend/src/state.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
from dataclasses import dataclass
|
||||
from sqlite3 import Cursor
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from .room import Room
|
||||
|
||||
|
||||
@dataclass
|
||||
class State:
|
||||
app: Flask
|
||||
db: Cursor
|
||||
rooms: dict[int, Room] = {} # { room_id: room, ... }
|
Loading…
Add table
Add a link
Reference in a new issue