29 lines
829 B
Python
29 lines
829 B
Python
# db.py
|
|
import os
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
import yaml
|
|
|
|
# db.py 파일 위치 기준 상위 디렉토리 (프로젝트 루트)
|
|
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
|
CONFIG_PATH = os.path.join(BASE_DIR, 'conf', 'config.yaml')
|
|
|
|
def load_config(path=CONFIG_PATH):
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
return yaml.safe_load(f)
|
|
|
|
config = load_config()
|
|
db_cfg = config['database']
|
|
|
|
db_url = f"mysql+pymysql://{db_cfg['user']}:{db_cfg['password']}@{db_cfg['host']}/{db_cfg['name']}?charset=utf8mb4"
|
|
|
|
# MySQL 연결이 끊겼을 때 자동 재시도 옵션 포함
|
|
engine = create_engine(db_url, pool_pre_ping=True)
|
|
Session = sessionmaker(bind=engine)
|
|
|
|
def get_engine():
|
|
return engine
|
|
|
|
def get_session():
|
|
return Session()
|