import asyncio from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import HTMLResponse from loguru import logger import sys import os if sys.platform == "win32": try: sys.stdout.reconfigure(encoding='utf-8') sys.stderr.reconfigure(encoding='utf-8') except AttributeError: pass from app.routes.health import router from app.workers.email_worker import email_loop from app.models.database import init_db # ── Logging setup ───────────────────────────────────────────────────────────── logger.remove() logger.add(sys.stdout, colorize=True, format="{time:HH:mm:ss} | {level} | {message}") logger.add("logs/agent.log", rotation="1 day", retention="7 days", level="INFO") # ── App ─────────────────────────────────────────────────────────────────────── app = FastAPI( title="AI Gmail Agent", description="Automatically categorizes, filters, and replies to emails using Groq LLM", version="2.0.0", ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) app.include_router(router) @app.on_event("startup") async def startup_event(): init_db() logger.info("✅ Database initialized") asyncio.create_task(email_loop()) @app.get("/", response_class=HTMLResponse) async def root(): if os.path.exists("index.html"): try: with open("index.html", "r", encoding="utf-8") as f: return HTMLResponse(content=f.read(), status_code=200) except Exception as e: logger.error(f"Error reading index.html: {e}") return """ AI Gmail Agent

AI Gmail Agent is Running 🚀

Ready to categorize, filter, and reply to your emails!

View stats at /api/stats

"""