45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
import logging
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
|
from app.config import settings
|
|
from app.models.announcement import Base
|
|
|
|
engine = create_async_engine(settings.database_url, echo=settings.debug)
|
|
async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
|
async def get_db() -> AsyncSession:
|
|
async with async_session() as session:
|
|
yield session
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
logging.basicConfig(
|
|
level=getattr(logging, settings.log_level),
|
|
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
|
)
|
|
from app.scheduler.jobs import start_scheduler, shutdown_scheduler
|
|
start_scheduler()
|
|
yield
|
|
shutdown_scheduler()
|
|
await engine.dispose()
|
|
|
|
|
|
app = FastAPI(
|
|
title="广西政府采购网公告监控系统",
|
|
version="2.0.0",
|
|
lifespan=lifespan,
|
|
docs_url="/docs" if settings.debug else None,
|
|
redoc_url=None,
|
|
)
|
|
|
|
from app.api.router import api_router
|
|
app.include_router(api_router)
|
|
|
|
|
|
@app.get("/health")
|
|
async def health():
|
|
return {"status": "ok"}
|