summaryrefslogtreecommitdiff
path: root/app/main.py
diff options
context:
space:
mode:
authorl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-03-17 14:11:45 +0200
committerl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-03-17 14:11:45 +0200
commit47fd81a5910eab3483f79d03eedf9307bc81252f (patch)
tree3e1ebd1bddcea649eb968e65161555afd4c2898c /app/main.py
parentf01cb6703710b7df4c7c022047cd35e1d5e9c70e (diff)
simple registration prototype
Diffstat (limited to 'app/main.py')
-rw-r--r--app/main.py82
1 files changed, 79 insertions, 3 deletions
diff --git a/app/main.py b/app/main.py
index fcb66a0..8b7d6eb 100644
--- a/app/main.py
+++ b/app/main.py
@@ -1,10 +1,86 @@
+import time
+
from fastapi import FastAPI
+from sqlalchemy import text
+
+from app.routes.routes import router as api_router
+from app.utils.create_tables import init_db
+from app.utils.db import engine
+from app.utils.logger_cfg import logger
+
+app_start_time = time.perf_counter()
+logger.debug("App start timestamp recorded")
app = FastAPI()
+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():
+ 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")
+
+ 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}")
+
+ elapsed = time.perf_counter() - app_start_time
+ logger.success(
+ f"Application startup completed in {elapsed*1000:.2f} ms ({elapsed:.6f} s)"
+ )
+ logger.debug("Startup complete timestamp recorded")
+
+
+@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}")
@app.get("/")
-def read_root():
+async def read_root():
+ logger.info("Root endpoint accessed")
+ logger.debug("Processing root endpoint request")
+ return {"message": "Hello new asyncpg ci!"}
+
+
+@app.get("/check-db")
+async def check_db():
+ logger.info("Database health check endpoint called")
+ logger.debug("Starting database health check")
+
+ try:
+ async with engine.begin() as conn:
+ logger.debug("Executing test query: SELECT 1")
+ result = await conn.execute(text("SELECT 1"))
+ db_result = result.scalar()
+ logger.info(f"Database health check successful: {db_result}")
+ logger.debug("Database health check query executed successfully")
+ return {"db_check": db_result}
- msg = "Hello new ci!"
- return {"message": msg}
+ except Exception as e:
+ logger.exception(f"Database health check failed: {e}")
+ raise