23 lines
628 B
Python
23 lines
628 B
Python
|
from sqlalchemy import create_engine
|
||
|
from sqlalchemy.orm import sessionmaker
|
||
|
from pathlib import Path
|
||
|
|
||
|
def get_db_path():
|
||
|
base_dir = Path(__file__).resolve().parent.parent.parent
|
||
|
data_dir = base_dir / 'data'
|
||
|
data_dir.mkdir(exist_ok=True)
|
||
|
return data_dir / 'vpn.db'
|
||
|
|
||
|
def init_db():
|
||
|
"""Initialize the database"""
|
||
|
from .models import Base
|
||
|
db_url = f"sqlite:///{get_db_path()}"
|
||
|
engine = create_engine(db_url)
|
||
|
Base.metadata.create_all(engine)
|
||
|
return engine
|
||
|
|
||
|
def get_session():
|
||
|
"""Get a database session"""
|
||
|
engine = init_db()
|
||
|
Session = sessionmaker(bind=engine)
|
||
|
return Session()
|