add configurable cloud api url and subpath routing support for reverse proxies

This commit is contained in:
2026-09-19 12:00:48 +05:30
parent ce16210525
commit cc45a2f270
10 changed files with 106 additions and 50 deletions
+15 -2
View File
@@ -127,6 +127,7 @@ async def lifespan(app: FastAPI):
print(f" Contec Ports: {settings.contec_ports}")
print(f" Monitor Model: {model}")
print(f" API Target: {settings.target_api_url}")
print(f" Cloud Sync: {getattr(settings, 'cloud_api_url', settings.target_api_url)}")
print(f" Database: {settings.database_url}")
print(f" ---------------------------------------------------------")
print(f" YOUR PC's IP ADDRESSES (configure on your monitor):")
@@ -196,22 +197,31 @@ app.add_middleware(
allow_headers=["*"],
)
# Include dashboard routes
# Include dashboard routes under multiple prefixes so reverse proxies (Nginx)
# work whether they strip /v1/contec-monitor or forward it as-is
app.include_router(dashboard.router, prefix="/api")
app.include_router(dashboard.router)
app.include_router(dashboard.router, prefix="/v1/contec-monitor/api")
app.include_router(dashboard.router, prefix="/v1/contec-monitor")
# Also serve dashboard at root and /dashboard directly for convenience
# Also serve dashboard directly for convenience across various path styles
from fastapi.responses import HTMLResponse
@app.get("/", response_class=HTMLResponse)
@app.get("/dashboard", response_class=HTMLResponse)
@app.get("/v1/contec-monitor", response_class=HTMLResponse)
@app.get("/v1/contec-monitor/dashboard", response_class=HTMLResponse)
@app.get("/v1/contec-monitor/api/dashboard", response_class=HTMLResponse)
def serve_dashboard_root():
return dashboard.serve_dashboard()
# Serve static folder
from fastapi.staticfiles import StaticFiles
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/v1/contec-monitor/static", StaticFiles(directory="static"), name="static_subpath")
from fastapi.responses import Response
@app.get("/favicon.ico", include_in_schema=False)
@app.get("/v1/contec-monitor/favicon.ico", include_in_schema=False)
async def favicon():
return Response(status_code=204, media_type="image/x-icon")
@@ -220,6 +230,9 @@ async def favicon():
# === WebSocket endpoint for live vitals ===
@app.websocket("/ws/vitals")
@app.websocket("/api/ws/vitals")
@app.websocket("/v1/contec-monitor/ws/vitals")
@app.websocket("/v1/contec-monitor/api/ws/vitals")
async def websocket_vitals(websocket: WebSocket):
manager = get_ws_manager()
await manager.connect(websocket)