from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager import uvicorn from datetime import datetime from app.api.endpoints import router from app.core.config import settings from app.core.database import close_mongo_connection, connect_to_mongo @asynccontextmanager async def lifespan(app: FastAPI): # 시작 시 print("News API service starting...") await connect_to_mongo() yield # 종료 시 print("News API service stopping...") await close_mongo_connection() app = FastAPI( title="News API Service", description="Multi-language news articles API service", version="1.0.0", lifespan=lifespan ) # CORS 설정 app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 라우터 등록 app.include_router(router, prefix="/api/v1") @app.get("/") async def root(): return { "service": "News API Service", "version": "1.0.0", "timestamp": datetime.now().isoformat(), "supported_languages": ["ko", "en", "zh_cn", "zh_tw", "ja", "fr", "de", "es", "it"], "endpoints": { "articles": "/api/v1/{lang}/articles", "article_by_id": "/api/v1/{lang}/articles/{article_id}", "latest": "/api/v1/{lang}/articles/latest", "search": "/api/v1/{lang}/articles/search?q=keyword" } } @app.get("/health") async def health_check(): return { "status": "healthy", "service": "news-api", "timestamp": datetime.now().isoformat() } if __name__ == "__main__": uvicorn.run( "main:app", host="0.0.0.0", port=8000, reload=True )