요청에 대한 명시적 로그 표출

This commit is contained in:
2026-01-27 03:00:07 +09:00
parent 7fb55a7be7
commit e27fbfada1

View File

@@ -1,5 +1,6 @@
import os
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import requests
from apscheduler.schedulers.background import BackgroundScheduler
@@ -26,6 +27,7 @@ WINDOWS_AI_SERVER_URL = os.getenv("WINDOWS_AI_SERVER_URL", "http://192.168.0.5:8
@app.on_event("startup")
def on_startup():
print(f"[StockLab] Startup. Windows AI Server URL: {WINDOWS_AI_SERVER_URL}")
init_db()
# 매일 아침 8시 뉴스 스크랩 (NAS 자체 수행)
@@ -75,15 +77,22 @@ def trigger_scrap():
@app.get("/api/trade/balance")
def get_balance():
"""계좌 잔고 조회 (Windows AI Server Proxy)"""
print(f"[Proxy] Requesting Balance from {WINDOWS_AI_SERVER_URL}...")
try:
# Windows Server로 잔고 조회 요청 (Windows가 직접 KIS 호출)
resp = requests.get(f"{WINDOWS_AI_SERVER_URL}/trade/balance", timeout=5)
# Windows Server가 500 등을 내더라도 JSON 포맷이면 그대로 전달
if resp.status_code != 200:
return {"error": "Windows AI Server returned error", "status": resp.status_code, "detail": resp.text}
print(f"[ProxyError] Balance Error: {resp.status_code} {resp.text}")
return JSONResponse(status_code=resp.status_code, content=resp.json())
print("[Proxy] Balance Success")
return resp.json()
except Exception as e:
return {"error": "Failed to connect to Windows AI Server", "detail": str(e), "url": WINDOWS_AI_SERVER_URL}
print(f"[ProxyError] Connection Failed: {e}")
return JSONResponse(
status_code=500,
content={"error": "Connection Failed", "detail": str(e), "target": WINDOWS_AI_SERVER_URL}
)
class OrderRequest(BaseModel):
code: str
@@ -94,32 +103,60 @@ class OrderRequest(BaseModel):
@app.post("/api/trade/order")
def order_stock(req: OrderRequest):
"""주식 매수/매도 주문 (Windows AI Server Proxy)"""
print(f"[Proxy] Order Request: {req.dict()} to {WINDOWS_AI_SERVER_URL}...")
try:
# Windows Server로 주문 요청
resp = requests.post(f"{WINDOWS_AI_SERVER_URL}/trade/order", json=req.dict(), timeout=10)
if resp.status_code != 200:
print(f"[ProxyError] Order Error: {resp.status_code} {resp.text}")
return JSONResponse(status_code=resp.status_code, content=resp.json())
return resp.json()
except Exception as e:
return {"success": False, "message": f"Proxy Error: {str(e)}", "url": WINDOWS_AI_SERVER_URL}
print(f"[ProxyError] Order Connection Failed: {e}")
return JSONResponse(
status_code=500,
content={"error": "Connection Failed", "detail": str(e), "target": WINDOWS_AI_SERVER_URL}
)
@app.post("/api/trade/auto")
def auto_trade():
"""AI 자동 매매 트리거 (Windows AI Server Proxy)"""
print(f"[Proxy] Triggering Auto Trade at {WINDOWS_AI_SERVER_URL}...")
try:
# Windows Server로 AI 매매 요청
resp = requests.post(f"{WINDOWS_AI_SERVER_URL}/trade/auto", timeout=120)
if resp.status_code != 200:
print(f"[ProxyError] Auto Trade Error: {resp.status_code} {resp.text}")
return JSONResponse(status_code=resp.status_code, content=resp.json())
print("[Proxy] Auto Trade Success")
return resp.json()
except Exception as e:
return {"success": False, "message": f"Proxy Error: {str(e)}", "url": WINDOWS_AI_SERVER_URL}
print(f"[ProxyError] Auto Trade Connection Failed: {e}")
return JSONResponse(
status_code=500,
content={"error": "Connection Failed", "detail": str(e), "target": WINDOWS_AI_SERVER_URL}
)
@app.get("/api/stock/analyze")
def analyze_market():
"""Windows PC를 통한 AI 시장 분석"""
print(f"[Proxy] Analyzing Market at {WINDOWS_AI_SERVER_URL}...")
try:
# Windows AI Server의 API 호출
resp = requests.post(f"{WINDOWS_AI_SERVER_URL}/analyze/portfolio", timeout=120)
if resp.status_code != 200:
print(f"[ProxyError] Analyze Error: {resp.status_code} {resp.text}")
return JSONResponse(status_code=resp.status_code, content=resp.json())
return resp.json()
except Exception as e:
return {"error": "Failed to connect to Windows AI Server", "detail": str(e), "url": WINDOWS_AI_SERVER_URL}
print(f"[ProxyError] Analyze Connection Failed: {e}")
return JSONResponse(
status_code=500,
content={"error": "Connection Failed", "detail": str(e), "target": WINDOWS_AI_SERVER_URL}
)
@app.get("/api/version")
def version():