63 lines
1.4 KiB
Python
63 lines
1.4 KiB
Python
from fastapi import FastAPI, Request, Response, WebSocket
|
|
|
|
from settings import settings
|
|
import auth
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get("/api/validate")
|
|
async def validate_token(token: str):
|
|
|
|
result = None
|
|
|
|
try:
|
|
data = auth.decode(token)
|
|
assert data["type"] == "frontend"
|
|
result = True
|
|
|
|
except Exception as e:
|
|
result = False
|
|
|
|
return {"success": result}
|
|
|
|
|
|
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))
|