Spaces:
Runtime error
Runtime error
| """ | |
| Database configuration and base models | |
| """ | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy.orm import sessionmaker | |
| import os | |
| # Get the database file path | |
| DB_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data', 'vpn.db') | |
| os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) | |
| # Create database engine | |
| SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_PATH}" | |
| engine = create_engine( | |
| SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} | |
| ) | |
| # Create SessionLocal class | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| # Create Base class | |
| Base = declarative_base() | |
| def get_db(): | |
| """Dependency to get database session""" | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |