MukeshKapoor25's picture
feat(logging): add centralized logger setup utility
206d2a8
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.sql_config import connect_to_database, disconnect_from_database
from app.routers.appointment import router as appointment_router
from app.routers.cart import router as cart_router
from app.auth.middleware import add_security_middleware
import logging
from app.utils.logger import setup_logger
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = setup_logger(__name__)
app = FastAPI(
title="Bookings / Appointment Management",
description="A microservice to manage bookings, appointments, and cart drafts",
version="1.0.0"
)
# Add CORS middleware for testing phase - permissive settings
app.add_middleware(
CORSMiddleware,
# Allow any localhost (IPv4/IPv6) with any port
allow_origin_regex=r"^https?://(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$",
allow_credentials=True,
allow_methods=["*"], # Allows all methods (GET, POST, PUT, DELETE, etc.)
allow_headers=["*"], # Allows all headers
)
# Add security middleware
add_security_middleware(app)
# Startup event to initialize database connection
@app.on_event("startup")
async def startup():
try:
await connect_to_database()
logger.info("Database connected successfully.")
# Log all registered routes
for route in app.router.routes:
logger.info(f"Registered route: {route.path}")
except Exception as e:
logger.error(f"Error connecting to the database: {e}")
# Shutdown event to close database connection
@app.on_event("shutdown")
async def shutdown():
try:
await disconnect_from_database()
logger.info("Database disconnected successfully.")
except Exception as e:
logger.error(f"Error disconnecting from the database: {e}")
# Include appointment router
app.include_router(
appointment_router,
prefix="/api/v1/appointments",
tags=["Appointments"],
)
# Include cart router
app.include_router(
cart_router,
prefix="/api/v1/cart",
tags=["Cart Management"],
)