Files
MAIA/backend/main.py
2025-04-21 23:47:38 +02:00

53 lines
1.6 KiB
Python

# main.py
from contextlib import _AsyncGeneratorContextManager, asynccontextmanager
from typing import Any, Callable
from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from core.database import get_engine, Base
from modules import router
import logging
# import all models to ensure they are registered before create_all
from modules.calendar.models import CalendarEvent
from modules.auth.models import User
from modules.todo.models import Todo # Import the new Todo model
logging.getLogger('passlib').setLevel(logging.ERROR) # fix bc package logging is broken
# Create DB tables (remove in production; use migrations instead)
def lifespan_factory() -> Callable[[FastAPI], _AsyncGeneratorContextManager[Any]]:
@asynccontextmanager
async def lifespan(app: FastAPI):
# Base.metadata.drop_all(bind=get_engine())
Base.metadata.create_all(bind=get_engine())
yield
return lifespan
lifespan = lifespan_factory()
app = FastAPI(lifespan=lifespan)
# Include module router
app.include_router(router)
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:8081", # Keep for web testing if needed
"http://192.168.1.9:8081", # Add your mobile device/emulator origin (adjust port if needed)
"http://192.168.255.221:8081",
# Add other origins if necessary, e.g., production frontend URL
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
# Health endpoint
@app.get("/api/health")
def health():
return {"status": "ok"}