데이터베이스 관련 파일을 conf 로 위치 변경
This commit is contained in:
21
conf/db.py
Normal file
21
conf/db.py
Normal file
@ -0,0 +1,21 @@
|
||||
# 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()
|
||||
Reference in New Issue
Block a user