from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
import logging
from app.database import engine, Base
from app.routers import events
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("Application is starting up...")
try:
logger.info("Creating database tables...")
Base.metadata.create_all(bind=engine)
logger.info("✓ Database tables created.")
except Exception as e:
logger.error(f"Failed to create database tables: {e}")
yield
logger.info("Application is shutting down...")
app = FastAPI(
title="Locate the News API",
description="Global news mapping with sentiment analysis",
version="1.0.0",
lifespan=lifespan
)
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=[
"*",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Routers
app.include_router(events.router, prefix="/api/v1", tags=["events"])
@app.get("/")
async def root():
return {
"message": "Locate the News API",
"version": "1.0.0",
"docs": "/docs"
}
@app.get("/health")
async def health():
return {"status": "healthy"}