summaryrefslogtreecommitdiff
path: root/app/main.py
diff options
context:
space:
mode:
authorl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-04-21 13:32:24 +0300
committerl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-04-21 13:32:24 +0300
commitf1842be3bfabe7850d33662da2da377676144c48 (patch)
tree95e1f5d6a72a2fc99847f0331907139b6b750dcb /app/main.py
parent70b0706973d9d856ca9f136df23a6fbec0901aea (diff)
uv migration
Diffstat (limited to 'app/main.py')
-rw-r--r--app/main.py52
1 files changed, 20 insertions, 32 deletions
diff --git a/app/main.py b/app/main.py
index 59ed4b7..f1c556a 100644
--- a/app/main.py
+++ b/app/main.py
@@ -1,4 +1,5 @@
import time
+from contextlib import asynccontextmanager
from fastapi import FastAPI
from sqlalchemy import text
@@ -12,58 +13,45 @@ from app.utils.logger_cfg import logger
app_start_time = time.perf_counter()
logger.debug("App start timestamp recorded")
-app = FastAPI()
-setup_cors(app)
-logger.info("FastAPI application instance created")
-logger.debug("FastAPI instance created successfully")
-
-app.include_router(api_router, prefix="/api")
-logger.info("API router registered with prefix /api")
-logger.debug("Router object: %s", api_router)
-
-@app.on_event("startup")
-async def startup():
+@asynccontextmanager
+async def lifespan(app: FastAPI):
logger.info("Application startup initiated")
- logger.debug("Starting database connection attempt")
try:
async with engine.begin() as conn:
logger.debug("Executing test query: SELECT 1")
- result = await conn.execute(text("SELECT 1"))
- db_status = result.scalar()
- logger.info(f"Database connection successful: {db_status}")
- logger.debug("Database test query executed successfully")
+ db_status = await conn.scalar(text("SELECT 1"))
+ logger.info("Database connection successful: {}", db_status)
logger.debug("Starting database table initialization")
await init_db()
logger.info("Database initialization completed")
- logger.debug("Database tables initialized successfully")
- except Exception as e:
- logger.exception(f"Database startup failed: {e}")
+ except Exception:
+ logger.exception("Database startup failed")
+ raise
elapsed = time.perf_counter() - app_start_time
+ app.state.startup_elapsed = elapsed
+ app.state.startup_completed = True
+
logger.success(
- f"Application startup completed in {elapsed*1000:.2f} ms ({elapsed:.6f} s)"
+ "Application startup completed in {:.2f} ms ({:.6f} s)",
+ elapsed * 1000,
+ elapsed,
)
- logger.debug("Startup complete timestamp recorded")
+ yield
-@app.on_event("shutdown")
-async def shutdown():
logger.info("Application shutdown initiated")
- logger.debug("Starting engine disposal")
try:
await engine.dispose()
logger.info("Database engine disposed successfully")
- logger.debug("Engine disposal finished")
- except Exception as e:
- logger.exception(f"Error during shutdown: {e}")
+ except Exception:
+ logger.exception("Error during shutdown")
-@app.get("/")
-async def read_root():
- logger.info("Root endpoint accessed")
- logger.debug("Processing root endpoint request")
- return {"message": "Hello new asyncpg ci!"}
+app = FastAPI(lifespan=lifespan)
+setup_cors(app)
+app.include_router(api_router, prefix="/api")