22 lines
570 B
Python
22 lines
570 B
Python
# db.py
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
import yaml
|
|
|
|
def load_config(path='conf/config.yaml'):
|
|
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_session():
|
|
return Session()
|