108 lines
3.1 KiB
Python
108 lines
3.1 KiB
Python
# src/web/api/v2/main.py
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import HTMLResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.templating import Jinja2Templates
|
|
from pathlib import Path
|
|
import logging
|
|
import time
|
|
|
|
from .settings import settings
|
|
from src.web.api.v2.routers.calibration_data import router as calibration_data_router
|
|
|
|
# --------------------------------------------------
|
|
# Logging
|
|
# --------------------------------------------------
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger("tradingbot.api.v2")
|
|
|
|
# --------------------------------------------------
|
|
# Base paths
|
|
# --------------------------------------------------
|
|
PROJECT_ROOT = settings.project_root
|
|
UI_ROOT = PROJECT_ROOT / "src/web/ui/v2"
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
# --------------------------------------------------
|
|
# FastAPI app
|
|
# --------------------------------------------------
|
|
app = FastAPI(
|
|
title=settings.api_title,
|
|
version=settings.api_version,
|
|
)
|
|
|
|
# --------------------------------------------------
|
|
# Middleware: request/response logging
|
|
# --------------------------------------------------
|
|
@app.middleware("http")
|
|
async def log_requests(request: Request, call_next):
|
|
start_time = time.time()
|
|
logger.info("➡️ %s %s", request.method, request.url.path)
|
|
response = await call_next(request)
|
|
elapsed_ms = (time.time() - start_time) * 1000
|
|
logger.info(
|
|
"⬅️ %s %s -> %s (%.1f ms)",
|
|
request.method,
|
|
request.url.path,
|
|
response.status_code,
|
|
elapsed_ms,
|
|
)
|
|
return response
|
|
|
|
# -------------------------
|
|
# Templates (UI v2)
|
|
# -------------------------
|
|
templates = Jinja2Templates(
|
|
directory=str(UI_ROOT / "templates")
|
|
)
|
|
|
|
# -------------------------
|
|
# Static files (UI v2)
|
|
# -------------------------
|
|
app.mount(
|
|
"/static",
|
|
StaticFiles(directory=str(UI_ROOT / "static")),
|
|
name="static",
|
|
)
|
|
|
|
# ==================================================
|
|
# ROUTES — UI ONLY (TEMPORAL)
|
|
# ==================================================
|
|
|
|
@app.get("/", response_class=HTMLResponse)
|
|
def trading_dashboard(request: Request):
|
|
return templates.TemplateResponse(
|
|
"pages/trading/dashboard.html",
|
|
{
|
|
"request": request,
|
|
"page": "trading",
|
|
},
|
|
)
|
|
|
|
@app.get("/calibration/data", response_class=HTMLResponse)
|
|
def calibration_data(request: Request):
|
|
return templates.TemplateResponse(
|
|
"pages/calibration/calibration_data.html",
|
|
{
|
|
"request": request,
|
|
"page": "calibration",
|
|
"step": 1,
|
|
},
|
|
)
|
|
|
|
# --------------------------------------------------
|
|
# API routers (versionados)
|
|
# --------------------------------------------------
|
|
api_prefix = settings.api_prefix
|
|
app.include_router(calibration_data_router, prefix=api_prefix)
|
|
|
|
return app
|
|
|
|
|
|
# ASGI app
|
|
app = create_app()
|
|
|
|
|