Update configuration and README for improved setup and security; disable default API and emoji sync, and enhance Discord permission checks in API routes.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
54
bot/api/db_manager.py
Normal file
54
bot/api/db_manager.py
Normal file
@@ -0,0 +1,54 @@
|
||||
# ╔══════════════════════════════════════════════════════════════════╗
|
||||
# ║ ║
|
||||
# ║ ░█▀▀░█▀█░█▀▄░█▀▀░█░█ ░█▀▄░█▀▀░█░█░█▀▀ ║
|
||||
# ║ ░█░░░█░█░█░█░█▀▀░▄▀▄ ░█░█░█▀▀░▀▄▀░▀▀█ ║
|
||||
# ║ ░▀▀▀░▀▀▀░▀▀░░▀▀▀░▀░▀ ░▀▀░░▀▀▀░░▀░░▀▀▀ ║
|
||||
# ║ ║
|
||||
# ║ © 2026 CodeX Devs — All Rights Reserved ║
|
||||
# ║ ║
|
||||
# ║ discord ── https://discord.gg/codexdev ║
|
||||
# ║ youtube ── https://youtube.com/@CodeXDevs ║
|
||||
# ║ github ── https://github.com/RayExo ║
|
||||
# ║ ║
|
||||
# ╚══════════════════════════════════════════════════════════════════╝
|
||||
|
||||
import aiosqlite
|
||||
import asyncio
|
||||
from typing import Dict
|
||||
|
||||
class DatabaseManager:
|
||||
"""
|
||||
A simple manager for persistent SQLite connections to avoid
|
||||
opening/closing files on every API request.
|
||||
"""
|
||||
def __init__(self):
|
||||
self._connections: Dict[str, aiosqlite.Connection] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def get_connection(self, db_path: str) -> aiosqlite.Connection:
|
||||
"""
|
||||
Retrieves an existing connection or creates a new one for the given path.
|
||||
"""
|
||||
async with self._lock:
|
||||
if db_path not in self._connections:
|
||||
# We use check_same_thread=False because aiosqlite handles
|
||||
# thread safety by running queries in a dedicated thread.
|
||||
conn = await aiosqlite.connect(db_path, check_same_thread=False)
|
||||
conn.row_factory = aiosqlite.Row
|
||||
self._connections[db_path] = conn
|
||||
return self._connections[db_path]
|
||||
|
||||
async def close_all(self):
|
||||
"""
|
||||
Closes all open connections. Called on API shutdown.
|
||||
"""
|
||||
async with self._lock:
|
||||
for path, conn in self._connections.items():
|
||||
try:
|
||||
await conn.close()
|
||||
except:
|
||||
pass
|
||||
self._connections.clear()
|
||||
|
||||
# Singleton instance
|
||||
db_manager = DatabaseManager()
|
||||
Reference in New Issue
Block a user