49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
import os
|
|
import logging
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from src.aggregate import aggregate_router
|
|
from src.config import config_router
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
from .database import setup_db_conn, get_config_from_db
|
|
from src.initialize import initialize_config
|
|
from .utils import setup_logging
|
|
|
|
logger = logging.getLogger("uvicorn")
|
|
logger.info("Starting application")
|
|
|
|
app = FastAPI()
|
|
app.include_router(aggregate_router)
|
|
app.include_router(config_router)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""make loading it async"""
|
|
log = logging.getLogger("uvicorn")
|
|
cfg_init_result = initialize_config()
|
|
|
|
shared_redis_conn = setup_db_conn(os.getenv("redis_host"), os.getenv("redis_port"))
|
|
if not shared_redis_conn:
|
|
log.error("Cannot connect to Redis DB. Exiting...")
|
|
exit(1)
|
|
|
|
inv_check = get_config_from_db(shared_redis_conn)
|
|
log.info(f"[DEBUG] Data validity healthcheck (DEVELOPER MODE): {inv_check}")
|
|
if not cfg_init_result:
|
|
log.error("Configuration initialization failed. Exiting...")
|
|
# exit(1)
|
|
|
|
yield
|
|
log.info("Shutting down FastAPI app...")
|
|
|
|
|
|
setup_logging()
|
|
log = logging.getLogger("uvicorn")
|
|
|
|
log.info("Starting FastAPI app...")
|
|
app = FastAPI(lifespan=lifespan)
|