Refactoring code for modular approach
This commit is contained in:
@@ -2,18 +2,21 @@ from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from sqlalchemy import text
|
||||
from .logging import setup_logging
|
||||
from .database import engine
|
||||
from .database import engine, SessionLocal
|
||||
from models.database_models import Base
|
||||
import logging
|
||||
from middleware.auth import AuthManager
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
auth_manager = AuthManager()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan events"""
|
||||
# Startup
|
||||
logger = setup_logging() # Configure logging first
|
||||
logger.info("Starting up ecommerce API")
|
||||
app_logger = setup_logging() # Configure logging first
|
||||
app_logger.info("Starting up ecommerce API")
|
||||
|
||||
# Create tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
@@ -21,10 +24,19 @@ async def lifespan(app: FastAPI):
|
||||
# Create indexes
|
||||
create_indexes()
|
||||
|
||||
# Create default admin user
|
||||
db = SessionLocal()
|
||||
try:
|
||||
auth_manager.create_default_admin_user(db)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to create default admin user: {e}")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
logger.info("Shutting down ecommerce API")
|
||||
app_logger.info("Shutting down ecommerce API")
|
||||
|
||||
|
||||
def create_indexes():
|
||||
|
||||
Reference in New Issue
Block a user