mirror of
https://github.com/sfiorini/iptv-server.git
synced 2026-04-11 10:40:45 +00:00
First commit
This commit is contained in:
26
src/utils/database.py
Normal file
26
src/utils/database.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import aiosqlite
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
DATABASE_PATH = os.getenv("DATABASE_PATH", "users.db")
|
||||
|
||||
# Ensure the directory for the database exists
|
||||
db_dir = os.path.dirname(DATABASE_PATH)
|
||||
if db_dir and not os.path.exists(db_dir):
|
||||
print(f"Creating directory for database: {DATABASE_PATH}")
|
||||
os.makedirs(db_dir, exist_ok=True)
|
||||
|
||||
|
||||
async def get_db():
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
# Enable WAL mode for better concurrency
|
||||
await db.execute("PRAGMA journal_mode=WAL;")
|
||||
await db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
username TEXT PRIMARY KEY,
|
||||
hashed_password TEXT
|
||||
)
|
||||
""")
|
||||
await db.commit()
|
||||
yield db
|
||||
Reference in New Issue
Block a user