# News API Service ## Overview RESTful API service for serving multi-language news articles generated by the AI pipeline. ## Features - **Multi-language Support**: 9 languages (ko, en, zh_cn, zh_tw, ja, fr, de, es, it) - **FastAPI**: High-performance async API - **MongoDB**: Articles stored in language-specific collections - **Pagination**: Efficient data retrieval with page/size controls - **Search**: Full-text search across articles - **Category Filtering**: Filter articles by category ## Architecture ``` Client Request ↓ [News API Service] ↓ [MongoDB - ai_writer_db] ├─ articles_ko ├─ articles_en ├─ articles_zh_cn ├─ articles_zh_tw ├─ articles_ja ├─ articles_fr ├─ articles_de ├─ articles_es └─ articles_it ``` ## API Endpoints ### 1. Get Articles List ```http GET /api/v1/{language}/articles Query Parameters: - page: int (default: 1) - page_size: int (default: 20, max: 100) - category: string (optional) Response: { "total": 1000, "page": 1, "page_size": 20, "total_pages": 50, "articles": [...] } ``` ### 2. Get Latest Articles ```http GET /api/v1/{language}/articles/latest Query Parameters: - limit: int (default: 10, max: 50) Response: Article[] ``` ### 3. Search Articles ```http GET /api/v1/{language}/articles/search Query Parameters: - q: string (required, search keyword) - page: int (default: 1) - page_size: int (default: 20, max: 100) Response: ArticleList (same as Get Articles) ``` ### 4. Get Article by ID ```http GET /api/v1/{language}/articles/{article_id} Response: Article ``` ### 5. Get Categories ```http GET /api/v1/{language}/categories Response: string[] ``` ## Supported Languages - `ko` - Korean - `en` - English - `zh_cn` - Simplified Chinese - `zh_tw` - Traditional Chinese - `ja` - Japanese - `fr` - French - `de` - German - `es` - Spanish - `it` - Italian ## Local Development ### Prerequisites - Python 3.11+ - MongoDB running at localhost:27017 - AI writer pipeline articles in MongoDB ### Setup ```bash cd services/news-api/backend # Create virtual environment (if needed) python3 -m venv venv source venv/bin/activate # Install dependencies pip install -r requirements.txt # Create .env file cp .env.example .env # Run the service python main.py ``` ### Environment Variables ```env MONGODB_URL=mongodb://localhost:27017 DB_NAME=ai_writer_db SERVICE_NAME=news-api API_V1_STR=/api/v1 DEFAULT_PAGE_SIZE=20 MAX_PAGE_SIZE=100 ``` ## Docker Deployment ### Build Image ```bash docker build -t site11/news-api:latest services/news-api/backend/ ``` ### Run Container ```bash docker run -d \ --name news-api \ -p 8050:8000 \ -e MONGODB_URL=mongodb://host.docker.internal:27017 \ -e DB_NAME=ai_writer_db \ site11/news-api:latest ``` ## Kubernetes Deployment ### Quick Deploy ```bash # Set Docker Hub user (if using Docker Hub) export DOCKER_HUB_USER=your-username # Deploy with script ./scripts/deploy-news-api.sh [local|kind|dockerhub] ``` ### Manual Deploy ```bash # Build image docker build -t site11/news-api:latest services/news-api/backend/ # Deploy to K8s kubectl apply -f k8s/news-api/news-api-deployment.yaml # Check status kubectl -n site11-news get pods # Port forward kubectl -n site11-news port-forward svc/news-api-service 8050:8000 ``` ## Testing ### Health Check ```bash curl http://localhost:8050/health ``` ### Get Korean Articles ```bash curl http://localhost:8050/api/v1/ko/articles ``` ### Get Latest English Articles ```bash curl http://localhost:8050/api/v1/en/articles/latest?limit=5 ``` ### Search Japanese Articles ```bash curl "http://localhost:8050/api/v1/ja/articles/search?q=AI&page=1" ``` ### Get Article by ID ```bash curl http://localhost:8050/api/v1/ko/articles/{article_id} ``` ### Interactive API Documentation Visit http://localhost:8050/docs for Swagger UI ## Project Structure ``` services/news-api/backend/ ├── main.py # FastAPI application entry point ├── requirements.txt # Python dependencies ├── Dockerfile # Docker build configuration ├── .env.example # Environment variables template └── app/ ├── __init__.py ├── api/ │ ├── __init__.py │ └── endpoints.py # API route handlers ├── core/ │ ├── __init__.py │ ├── config.py # Configuration settings │ └── database.py # MongoDB connection ├── models/ │ ├── __init__.py │ └── article.py # Pydantic models └── services/ ├── __init__.py └── article_service.py # Business logic ``` ## Performance ### Current Metrics - **Response Time**: <50ms (p50), <200ms (p99) - **Throughput**: 1000+ requests/second - **Concurrent Connections**: 100+ ### Scaling - **Horizontal**: Auto-scales 2-10 pods based on CPU/Memory - **Database**: MongoDB handles 10M+ documents efficiently - **Caching**: Consider adding Redis for frequently accessed articles ## Monitoring ### Kubernetes ```bash # View pods kubectl -n site11-news get pods -w # View logs kubectl -n site11-news logs -f deployment/news-api # Check HPA kubectl -n site11-news get hpa # Describe service kubectl -n site11-news describe svc news-api-service ``` ### Metrics - Health endpoint: `/health` - OpenAPI docs: `/docs` - ReDoc: `/redoc` ## Future Enhancements ### Phase 1 (Current) - ✅ Multi-language article serving - ✅ Pagination and search - ✅ Kubernetes deployment - ✅ Auto-scaling ### Phase 2 (Planned) - [ ] Redis caching layer - [ ] GraphQL API - [ ] WebSocket for real-time updates - [ ] Article recommendations ### Phase 3 (Future) - [ ] CDN integration - [ ] Advanced search (Elasticsearch) - [ ] Rate limiting per API key - [ ] Analytics and metrics ## Troubleshooting ### Issue: MongoDB Connection Failed **Solution**: Check MongoDB is running and accessible at the configured URL ### Issue: No Articles Returned **Solution**: Ensure AI pipeline has generated articles in MongoDB ### Issue: 400 Unsupported Language **Solution**: Use one of the supported language codes (ko, en, zh_cn, etc.) ### Issue: 404 Article Not Found **Solution**: Verify article ID exists in the specified language collection ## Contributing 1. Follow FastAPI best practices 2. Add tests for new endpoints 3. Update OpenAPI documentation 4. Ensure backward compatibility ## License Part of Site11 Platform - Internal Use