aboutsummaryrefslogtreecommitdiff
path: root/app/main.py
blob: 495e41f7d40d3c0db872fc8c7e06186d6e4b8fc8 (plain) (blame)
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
from contextlib import asynccontextmanager

from fastapi import FastAPI
from sqlalchemy import text

from app.db import engine, Base
from app.jobs import start_scheduler, scheduler
from app.routers.health import router as health_router
from app.routers.devices import router as devices_router
from app.routers.subscriptions import router as subscriptions_router
from app.routers.test import router as test_router


@asynccontextmanager
async def lifespan(app: FastAPI):
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
        await conn.execute(text("select 1"))
    start_scheduler()
    yield
    if scheduler.running:
        scheduler.shutdown(wait=False)
    await engine.dispose()


app = FastAPI(title="hutch-notify", lifespan=lifespan)
app.include_router(health_router)
app.include_router(devices_router)
app.include_router(subscriptions_router)
app.include_router(test_router)