72 lines
1.7 KiB
Python
72 lines
1.7 KiB
Python
import dotenv
|
|
from fastapi import FastAPI
|
|
from contextlib import asynccontextmanager
|
|
import logging
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
# db
|
|
from database import Base, engine
|
|
|
|
# IMPORTANT: Import all models to register them with SQLAlchemy
|
|
# from models.Clinics import Clinics
|
|
# from models.Doctors import Doctors
|
|
# from models.Patients import Patients
|
|
# from models.Appointments import Appointments
|
|
# from models.Calendar import Calenders
|
|
|
|
# routers
|
|
from apis import api_router
|
|
|
|
# middleware
|
|
from middleware.ErrorHandlerMiddleware import ErrorHandlerMiddleware, configure_exception_handlers
|
|
|
|
dotenv.load_dotenv()
|
|
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
logger.info("Starting application")
|
|
try:
|
|
Base.metadata.create_all(bind=engine)
|
|
logger.info("Created database tables")
|
|
except Exception as e:
|
|
logger.error(f"Error creating database tables: {e}")
|
|
raise e
|
|
yield
|
|
logger.info("Stopping application")
|
|
|
|
|
|
app = FastAPI(
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # Allows all origins
|
|
allow_credentials=True,
|
|
allow_methods=["*"], # Allows all methods
|
|
allow_headers=["*"], # Allows all headers
|
|
)
|
|
|
|
# Error handler middleware
|
|
app.add_middleware(ErrorHandlerMiddleware)
|
|
|
|
# Configure exception handlers
|
|
configure_exception_handlers(app)
|
|
|
|
@app.get("/")
|
|
async def hello_world():
|
|
return {"Hello": "World"}
|
|
|
|
|
|
# Routes
|
|
app.include_router(api_router, prefix="/api")
|