-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
46 lines (34 loc) · 1.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.staticfiles import StaticFiles
from starlette.responses import PlainTextResponse
from src.dependecies import get_settings, get_admin_publisher, get_feed_publisher
from src.routes import router
from src.tasks.fetcher import Fetcher
from src.tasks.listener import WebSocketListener
# Start background tasks when the app starts
@asynccontextmanager
async def lifespan(_app: FastAPI):
settings = await get_settings()
feed_publisher = await get_feed_publisher()
admin_publisher = await get_admin_publisher()
asyncio.create_task(WebSocketListener(settings, feed_publisher, admin_publisher).start())
asyncio.create_task(Fetcher(settings, feed_publisher, admin_publisher).fetch())
await feed_publisher.publish("frozen", settings.site.freeze is not None)
await feed_publisher.publish("message", settings.site.message)
await admin_publisher.publish("active-source", settings.source.dict())
yield # Signal that the startup can go ahead
app = FastAPI(
title="Loxsi",
description="Data proxy for the Telraam application",
version="0.6.9",
lifespan=lifespan,
)
app.mount("/static", StaticFiles(directory="static"), name="static")
app.include_router(router)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
print(exc.with_traceback(), flush=True)
return PlainTextResponse(str(exc), status_code=400)