controlpanel-old/backend/main.py

70 lines
1.8 KiB
Python

from fastapi import FastAPI, Request, Response, WebSocket, WebSocketDisconnect
from settings import settings
import auth
from state import StateManager
app = FastAPI()
state_manager = StateManager()
@app.get("/api/{token}/validate")
async def validate_token(token: str):
return {"success": auth.validate_frontend(token)}
@app.websocket("/api/{token}/state")
async def state_updates_websocket(websocket: WebSocket, token: str):
if not auth.validate_frontend(token):
await websocket.close()
return
await websocket.accept()
await state_manager.on_connect(websocket)
try:
while True:
await websocket.receive_json()
except WebSocketDisconnect:
await state_manager.on_disconnect(websocket)
if settings.dev_mode:
print("Starting in development mode.")
print(f"Proxying requests to npm server on localhost:{settings.dev_npm_port}")
import httpx
@app.get("/{path:path}")
async def dev_mode_proxy(path: str, response: Response):
async with httpx.AsyncClient() as proxy:
proxy_response = await proxy.get(
f"http://localhost:{settings.dev_npm_port}/{path}"
)
response.body = proxy_response.content
response.status_code = proxy_response.status_code
return response
else:
print("Starting in production mode")
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
@app.middleware("http")
async def index_catch_all(request: Request, call_next):
response = await call_next(request)
if response.status_code == 404:
return FileResponse(f"{settings.frontend_path}/index.html")
return response
app.mount("/", StaticFiles(directory=settings.frontend_path))