SplatNet-Backend/main.py

56 lines
1.7 KiB
Python
Raw Normal View History

2026-02-06 23:06:27 +01:00
from fastapi import FastAPI, Request
from routes import sso, user
from config import settings
import uvicorn
2026-02-06 23:06:27 +01:00
from fastapi.middleware.cors import CORSMiddleware
2026-02-11 03:45:44 +01:00
from database import init_db
2026-02-11 04:46:12 +01:00
from routes import logout
2026-02-19 06:08:41 +01:00
from routes import boss
from services.boss_retrieval import process_boss_file
from contextlib import asynccontextmanager
import asyncio
2026-02-19 06:08:41 +01:00
async def boss_worker_loop():
print("background worker started")
while True:
try:
print("running boss service")
process_boss_file()
except Exception as e:
print(f"worker error: {e}")
await asyncio.sleep(3600)
@asynccontextmanager
async def lifespan(app: FastAPI):
task = asyncio.create_task(boss_worker_loop())
yield
print("shutdown: cancelling background tasks")
task.cancel()
app = FastAPI(lifespan=lifespan)
2026-02-20 04:44:33 +01:00
app.add_middleware(
CORSMiddleware,
allow_origins=[settings.frontend_url],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["Set-Cookie"],
)
2026-02-06 23:06:27 +01:00
@app.middleware("http")
async def force_cors_on_errors(request: Request, call_next):
response = await call_next(request)
origin = request.headers.get("origin")
if origin == settings.frontend_url:
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Access-Control-Allow-Credentials"] = "true"
return response
2026-02-06 01:34:21 +01:00
app.include_router(sso.router, prefix="/api/v2/sso")
2026-02-06 23:06:27 +01:00
app.include_router(user.router, prefix="/api/v1/users")
2026-02-11 04:46:12 +01:00
app.include_router(logout.router, prefix="/api/v1")
2026-02-19 06:08:41 +01:00
app.include_router(boss.router, prefix="/api/v1")
if __name__ == '__main__':
2026-02-19 06:08:41 +01:00
uvicorn.run("main:app", host="0.0.0.0", port=settings.port, reload=True)