Migrate to motor for DB interaction
This commit is contained in:
@@ -1,40 +1,29 @@
|
||||
import logging
|
||||
import sys
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi import FastAPI
|
||||
|
||||
from mongoengine import connect
|
||||
|
||||
from app.config import get_settings
|
||||
from database.utils import create_admin_user
|
||||
from routes import users, flights
|
||||
from routes import users, flights, auth
|
||||
|
||||
logger = logging.getLogger("api")
|
||||
|
||||
logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s', level=logging.DEBUG)
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
logger.addHandler(handler)
|
||||
|
||||
|
||||
async def connect_to_db():
|
||||
# Connect to MongoDB
|
||||
settings = get_settings()
|
||||
try:
|
||||
connected = connect(settings.db_name, host=settings.db_uri, username=settings.db_user,
|
||||
password=settings.db_pwd, authentication_source=settings.db_name)
|
||||
if connected:
|
||||
logging.info("Connected to database %s", settings.db_name)
|
||||
# Create default admin user if it doesn't exist
|
||||
create_admin_user()
|
||||
except ConnectionError:
|
||||
logger.error("Failed to connect to MongoDB")
|
||||
raise ConnectionError
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
await create_admin_user()
|
||||
yield
|
||||
|
||||
|
||||
# Initialize FastAPI
|
||||
app = FastAPI()
|
||||
app.include_router(users.router)
|
||||
app.include_router(flights.router)
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
await connect_to_db()
|
||||
# Add subroutes
|
||||
app.include_router(users.router, tags=["Users"], prefix="/users")
|
||||
app.include_router(flights.router, tags=["Flights"], prefix="/flights")
|
||||
app.include_router(auth.router, tags=["Auth"], prefix="/auth")
|
||||
|
Reference in New Issue
Block a user