Move CORM settings and histories to dedicated PostgreSQL database
This commit is contained in:
@@ -9,7 +9,7 @@ import re
|
||||
import io
|
||||
import math
|
||||
import datetime
|
||||
import configparser
|
||||
import functools
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
import uuid
|
||||
@@ -29,12 +29,18 @@ from naver_api import get_access_token, get_payment_done_orders, get_product_ord
|
||||
import cafe24_api
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional, Dict, Any
|
||||
from google_runtime import (
|
||||
GOOGLE_READ_TIMEOUT, GoogleTimeoutSession, SettingsCache,
|
||||
create_google_client, google_worker, history_access, run_google_io, settings_update,
|
||||
create_google_client, google_worker, history_access as google_history_access, run_google_io, settings_update,
|
||||
)
|
||||
from settings_store import (
|
||||
PostgresSettingsStore, SettingsConflict, SettingsNotInitialized,
|
||||
config_to_rows, rows_to_config, settings_db_config,
|
||||
)
|
||||
from history_store import PostgresHistoryStore
|
||||
|
||||
# ==========================================
|
||||
# SSO / 서브경로 호스팅 설정
|
||||
@@ -58,6 +64,15 @@ DB_CONFIG = {
|
||||
'user': _PG_USER,
|
||||
'password': _PG_PASSWORD,
|
||||
}
|
||||
SETTINGS_BACKEND = os.getenv("SETTINGS_BACKEND", "google").strip().lower()
|
||||
if SETTINGS_BACKEND not in {"google", "postgres"}:
|
||||
raise RuntimeError("SETTINGS_BACKEND는 google 또는 postgres여야 합니다.")
|
||||
SETTINGS_DB_CONFIG = settings_db_config()
|
||||
_postgres_settings = PostgresSettingsStore(SETTINGS_DB_CONFIG)
|
||||
HISTORY_BACKEND = os.getenv("HISTORY_BACKEND", "google").strip().lower()
|
||||
if HISTORY_BACKEND not in {"google", "postgres"}:
|
||||
raise RuntimeError("HISTORY_BACKEND는 google 또는 postgres여야 합니다.")
|
||||
_postgres_history = PostgresHistoryStore(SETTINGS_DB_CONFIG)
|
||||
|
||||
# 2. 반품 데이터 저장용 DB
|
||||
RETURN_DB_CONFIG = {
|
||||
@@ -193,9 +208,58 @@ def _load_settings_rows():
|
||||
return get_settings_worksheet().get_all_values()
|
||||
|
||||
|
||||
def settings_worker(function):
|
||||
"""DB settings must remain available even when Google workers are saturated."""
|
||||
@functools.wraps(function)
|
||||
async def wrapped(*args, **kwargs):
|
||||
if SETTINGS_BACKEND == "postgres":
|
||||
return await run_in_threadpool(function, *args, **kwargs)
|
||||
return await run_google_io(function, *args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
def history_worker(function):
|
||||
@functools.wraps(function)
|
||||
async def wrapped(*args, **kwargs):
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
return await run_in_threadpool(function, *args, **kwargs)
|
||||
return await run_google_io(function, *args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
def history_access(function):
|
||||
legacy = google_history_access(function)
|
||||
|
||||
@functools.wraps(function)
|
||||
def wrapped(*args, **kwargs):
|
||||
# DB records use stable IDs and DB transactions, so a slow Google order
|
||||
# cancellation must not hold up unrelated history reads and saves.
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
return function(*args, **kwargs)
|
||||
return legacy(*args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
def history_db_call(method, *args, **kwargs):
|
||||
try:
|
||||
return getattr(_postgres_history, method)(*args, **kwargs)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail="내역의 일시와 입력 내용을 확인해주세요.") from exc
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=503, detail="DB 내역 처리를 확인하지 못했습니다. 새로고침하여 확인해주세요.") from exc
|
||||
|
||||
|
||||
def read_config(force_refresh=False):
|
||||
config = configparser.ConfigParser(allow_no_value=True)
|
||||
config.optionxform = str
|
||||
if SETTINGS_BACKEND == "postgres":
|
||||
try:
|
||||
rows, version = _postgres_settings.read()
|
||||
config = rows_to_config(rows)
|
||||
config._settings_version = version
|
||||
return config
|
||||
except SettingsNotInitialized as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=503, detail="DB에서 설정을 읽지 못했습니다. 잠시 후 다시 시도해주세요.") from exc
|
||||
|
||||
try:
|
||||
rows = _settings_cache.read(_load_settings_rows, force=force_refresh)
|
||||
@@ -205,44 +269,23 @@ def read_config(force_refresh=False):
|
||||
detail="구글에서 최신 설정을 읽지 못했습니다. 잠시 후 다시 시도해주세요.",
|
||||
) from exc
|
||||
|
||||
if rows and len(rows) > 1:
|
||||
for row in rows:
|
||||
if not row or not row[0].strip():
|
||||
continue
|
||||
section = row[0].strip()
|
||||
if section == "대분류 (Section)":
|
||||
continue
|
||||
|
||||
key = row[1].strip() if len(row) > 1 else ""
|
||||
|
||||
c_cells = [x.strip() for x in row[2:]]
|
||||
while c_cells and not c_cells[-1]:
|
||||
c_cells.pop()
|
||||
val = " | ".join(c_cells)
|
||||
|
||||
if not config.has_section(section):
|
||||
config.add_section(section)
|
||||
|
||||
if key:
|
||||
if val:
|
||||
config.set(section, key, val)
|
||||
else:
|
||||
config.set(section, key)
|
||||
return config
|
||||
return rows_to_config(rows)
|
||||
|
||||
def write_config(config):
|
||||
rows = [["대분류 (Section)", "항목 (Key)", "설정값 1", "설정값 2", "설정값 3", "설정값 4", "설정값 5"]]
|
||||
for section in config.sections():
|
||||
items = config.items(section)
|
||||
if not items:
|
||||
rows.append([section, "", ""])
|
||||
else:
|
||||
for k, v in items:
|
||||
if v is None:
|
||||
rows.append([section, k])
|
||||
else:
|
||||
v_parts = [p.strip() for p in v.split("|")]
|
||||
rows.append([section, k] + v_parts)
|
||||
rows = config_to_rows(config)
|
||||
if SETTINGS_BACKEND == "postgres":
|
||||
try:
|
||||
config._settings_version = _postgres_settings.write(
|
||||
rows, getattr(config, "_settings_version", None),
|
||||
)
|
||||
except SettingsConflict as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
except Exception as exc:
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail="DB 설정 저장을 확인하지 못했습니다. 새로고침하여 저장 여부를 확인해주세요.",
|
||||
) from exc
|
||||
return
|
||||
|
||||
try:
|
||||
ws = get_settings_worksheet()
|
||||
@@ -298,7 +341,9 @@ async def index(request: Request):
|
||||
|
||||
@app.get("/api/config")
|
||||
async def get_config():
|
||||
if _settings_cache.has_value:
|
||||
if SETTINGS_BACKEND == "postgres":
|
||||
config = await run_in_threadpool(read_config)
|
||||
elif _settings_cache.has_value:
|
||||
config = read_config()
|
||||
else:
|
||||
config = await run_google_io(read_config)
|
||||
@@ -307,7 +352,8 @@ async def get_config():
|
||||
data[section] = dict(config.items(section))
|
||||
data[section]["__order"] = list(config.options(section))
|
||||
return JSONResponse(data, headers={
|
||||
"X-Settings-Cache": "stale" if _settings_cache.stale else "fresh",
|
||||
"X-Settings-Cache": "none" if SETTINGS_BACKEND == "postgres" else ("stale" if _settings_cache.stale else "fresh"),
|
||||
"X-Settings-Backend": SETTINGS_BACKEND,
|
||||
})
|
||||
|
||||
|
||||
@@ -337,7 +383,7 @@ class SubgroupNamePayload(BaseModel):
|
||||
name: str = ""
|
||||
|
||||
@app.post("/api/products/settings")
|
||||
@google_worker
|
||||
@settings_worker
|
||||
@settings_update
|
||||
def save_products_settings(payload: ProductsPayload):
|
||||
config = read_config(force_refresh=True)
|
||||
@@ -377,7 +423,7 @@ def save_products_settings(payload: ProductsPayload):
|
||||
return {"status": "success"}
|
||||
|
||||
@app.post("/api/products/subgroup-name")
|
||||
@google_worker
|
||||
@settings_worker
|
||||
@settings_update
|
||||
def save_product_subgroup_name(payload: SubgroupNamePayload):
|
||||
group_key = payload.group_key.strip()
|
||||
@@ -527,11 +573,16 @@ async def startup_event():
|
||||
print(f"[Warning] DB 초기화 실패. CONTINUE_WITHOUT_DB=1 이므로 서버는 계속 시작합니다: {e}")
|
||||
else:
|
||||
raise
|
||||
if env_bool("SKIP_GOOGLE_INIT", False):
|
||||
if SETTINGS_BACKEND == "postgres":
|
||||
# Fail visibly on incomplete migration instead of falling back to old sheet values.
|
||||
await run_in_threadpool(_postgres_settings.read)
|
||||
elif env_bool("SKIP_GOOGLE_INIT", False):
|
||||
print("SKIP_GOOGLE_INIT=1 이므로 구글 시트 선초기화를 건너뜁니다.")
|
||||
else:
|
||||
# Google unavailability must not delay startup or the DB-backed tabs.
|
||||
_settings_cache.refresh_in_background(_load_settings_rows)
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
await run_in_threadpool(_postgres_history.check_ready)
|
||||
|
||||
@app.post("/api/order/submit")
|
||||
@google_worker
|
||||
@@ -660,9 +711,11 @@ def download_and_clear_sheet():
|
||||
|
||||
|
||||
@app.get("/api/sms/history")
|
||||
@google_worker
|
||||
@history_worker
|
||||
@history_access
|
||||
def get_history():
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
return history_db_call("list_records", "sms")
|
||||
local_history = {}
|
||||
def merge_history_item(sheet_item, local_item):
|
||||
if not local_item:
|
||||
@@ -727,9 +780,11 @@ def get_history():
|
||||
return []
|
||||
|
||||
@app.post("/api/sms/history")
|
||||
@google_worker
|
||||
@history_worker
|
||||
@history_access
|
||||
def add_history(data: dict):
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
return {"status": "success", "id": history_db_call("add", "sms", data)}
|
||||
with open(SMS_HISTORY_FILE, 'a', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False)
|
||||
f.write('\n')
|
||||
@@ -768,9 +823,12 @@ def add_history(data: dict):
|
||||
return {"status": "success"}
|
||||
|
||||
@app.delete("/api/sms/history")
|
||||
@google_worker
|
||||
@history_worker
|
||||
@history_access
|
||||
def clear_history():
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
history_db_call("clear_sms")
|
||||
return {"status": "success"}
|
||||
if os.path.exists(SMS_HISTORY_FILE):
|
||||
os.remove(SMS_HISTORY_FILE)
|
||||
|
||||
@@ -782,10 +840,23 @@ def clear_history():
|
||||
|
||||
return {"status": "success"}
|
||||
|
||||
@app.delete("/api/sms/history/records/{record_id}")
|
||||
@history_worker
|
||||
@history_access
|
||||
def delete_sms_history_record(record_id: int):
|
||||
if HISTORY_BACKEND != "postgres":
|
||||
raise HTTPException(status_code=409, detail="내역 화면을 새로고침해주세요.")
|
||||
if not history_db_call("delete", "sms", record_id):
|
||||
raise HTTPException(status_code=404, detail="이미 삭제된 내역입니다. 새로고침해주세요.")
|
||||
return {"status": "success"}
|
||||
|
||||
|
||||
@app.delete("/api/sms/history/{index}")
|
||||
@google_worker
|
||||
@history_worker
|
||||
@history_access
|
||||
def delete_history_item(index: int):
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
raise HTTPException(status_code=409, detail="내역 화면을 새로고침한 후 다시 삭제해주세요.")
|
||||
try:
|
||||
ws = get_sms_history_worksheet()
|
||||
rows = ws.get_all_values()
|
||||
@@ -820,11 +891,13 @@ def delete_history_item(index: int):
|
||||
import sys
|
||||
|
||||
@app.get("/api/manual/history")
|
||||
@google_worker
|
||||
@history_worker
|
||||
@history_access
|
||||
def get_manual_history():
|
||||
korea_tz = datetime.timezone(datetime.timedelta(hours=9))
|
||||
today_prefix = datetime.datetime.now(korea_tz).strftime("%Y-%m-%d")
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
return history_db_call("list_records", "manual", day=today_prefix)
|
||||
|
||||
local_history = {}
|
||||
if os.path.exists(MANUAL_HISTORY_FILE):
|
||||
@@ -868,9 +941,11 @@ def get_manual_history():
|
||||
return list(local_history.values())
|
||||
|
||||
@app.post("/api/manual/history")
|
||||
@google_worker
|
||||
@history_worker
|
||||
@history_access
|
||||
def add_manual_history(data: dict):
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
return {"status": "success", "id": history_db_call("add", "manual", data)}
|
||||
local_history = []
|
||||
korea_tz = datetime.timezone(datetime.timedelta(hours=9))
|
||||
today_prefix = datetime.datetime.now(korea_tz).strftime("%Y-%m-%d")
|
||||
@@ -906,10 +981,43 @@ def add_manual_history(data: dict):
|
||||
|
||||
return {"status": "success"}
|
||||
|
||||
def delete_manual_order_rows(timestamp):
|
||||
"""Keep cancellation of the active Google order queue, separate from history."""
|
||||
ws = get_manual_worksheet()
|
||||
rows = ws.get_all_values()
|
||||
# Delete exact matching rows in one request, from bottom to top.
|
||||
requests = [{"deleteDimension": {"range": {
|
||||
"sheetId": ws.id, "dimension": "ROWS", "startIndex": index, "endIndex": index + 1,
|
||||
}}} for index in reversed(range(1, len(rows))) if rows[index] and rows[index][0] == timestamp]
|
||||
if requests:
|
||||
ws.spreadsheet.batch_update({"requests": requests})
|
||||
|
||||
|
||||
@app.delete("/api/manual/history/records/{record_id}")
|
||||
@google_worker
|
||||
@google_history_access
|
||||
def delete_manual_history_record(record_id: int):
|
||||
if HISTORY_BACKEND != "postgres":
|
||||
raise HTTPException(status_code=409, detail="내역 화면을 새로고침해주세요.")
|
||||
record = history_db_call("get_manual", record_id)
|
||||
if record is None:
|
||||
raise HTTPException(status_code=404, detail="이미 삭제된 내역입니다. 새로고침해주세요.")
|
||||
if history_db_call("manual_timestamp_count", record["timestamp"]) > 1:
|
||||
raise HTTPException(status_code=409, detail="같은 일시의 수동발주가 여러 건입니다. 발주 시트에서 대상 주문을 먼저 확인해주세요.")
|
||||
try:
|
||||
delete_manual_order_rows(record["timestamp"])
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=503, detail="발주 시트 삭제를 확인하지 못했습니다. 내역을 보존했습니다. 발주 시트 확인 후 다시 시도해주세요.") from exc
|
||||
history_db_call("delete", "manual", record_id)
|
||||
return {"status": "success"}
|
||||
|
||||
|
||||
@app.delete("/api/manual/history/{timestamp}")
|
||||
@google_worker
|
||||
@history_access
|
||||
def delete_manual_history(timestamp: str):
|
||||
if HISTORY_BACKEND == "postgres":
|
||||
raise HTTPException(status_code=409, detail="내역 화면을 새로고침한 후 다시 삭제해주세요.")
|
||||
# 구글 시트에서 삭제
|
||||
try:
|
||||
ws = get_manual_worksheet()
|
||||
@@ -980,7 +1088,7 @@ async def get_fonts():
|
||||
return fonts_list
|
||||
|
||||
@app.post("/api/gift/settings")
|
||||
@google_worker
|
||||
@settings_worker
|
||||
@settings_update
|
||||
def save_gift_settings(payload: dict):
|
||||
# 하위 호환성 (기존 rules만 오는 경우)
|
||||
@@ -1016,7 +1124,7 @@ def save_gift_settings(payload: dict):
|
||||
return {"status": "success"}
|
||||
|
||||
@app.post("/api/app/settings")
|
||||
@google_worker
|
||||
@settings_worker
|
||||
@settings_update
|
||||
def save_app_settings(payload: dict):
|
||||
config = read_config(force_refresh=True)
|
||||
|
||||
Reference in New Issue
Block a user