Spaces:
Running
Running
File size: 1,931 Bytes
31ca2a2 8bb2b58 31ca2a2 8bb2b58 31ca2a2 8bb2b58 31ca2a2 8bb2b58 31ca2a2 8bb2b58 31ca2a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from pydantic import BaseModel
from . import router
# -------------------------------------------------------
# FastAPI app
# -------------------------------------------------------
app = FastAPI(title="Stock / Index Backend")
# -------------------------------------------------------
# Middleware
# -------------------------------------------------------
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(GZipMiddleware, minimum_size=1000)
# -------------------------------------------------------
# Request model
# -------------------------------------------------------
class FetchRequest(BaseModel):
mode: str
req_type: str
name: str = ""
date_start: str = ""
date_end: str = ""
# -------------------------------------------------------
# Health
# -------------------------------------------------------
@app.get("/")
def health():
return {"status": "ok", "service": "backend alive"}
# -------------------------------------------------------
# Main API
# -------------------------------------------------------
@app.post("/api/fetch", response_class=HTMLResponse)
def fetch_data(req: FetchRequest):
mode = req.mode.lower()
# ✅ Used by frontend on page load
if mode == "list":
return HTMLResponse(content=router.build_req_type_list_html())
if mode == "stock":
html = router.handle_stock(req)
elif mode == "index":
html = router.handle_index(req)
elif mode == "screener":
html = router.handle_screener(req)
else:
raise HTTPException(status_code=400, detail="Invalid mode")
# 🔒 Always return HTML
return HTMLResponse(content=str(html))
|