Spaces:
Paused
Paused
| """Main FastAPI application""" | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from contextlib import asynccontextmanager | |
| import time | |
| import logging | |
| from app.api.endpoints import scraping | |
| from app.api.models.response import HealthResponse | |
| from app.core.config import settings | |
| from app.utils.logger import setup_logging | |
| # Setup logging | |
| setup_logging() | |
| logger = logging.getLogger(__name__) | |
| # Track application start time | |
| app_start_time = time.time() | |
| async def lifespan(app: FastAPI): | |
| """Manage application lifecycle""" | |
| logger.info("Starting ERUMESU Scraper API...") | |
| logger.info(f"BrightData URL configured: {settings.BRIGHTDATA_HOST}") | |
| yield | |
| logger.info("Shutting down ERUMESU Scraper API...") | |
| # Create FastAPI application | |
| app = FastAPI( | |
| title=settings.PROJECT_NAME, | |
| version="1.0.0", | |
| description="API for scraping Hermes products using BrightData Browser API", | |
| lifespan=lifespan | |
| ) | |
| # Configure CORS | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Configure appropriately for production | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Include routers | |
| app.include_router( | |
| scraping.router, | |
| prefix=f"{settings.API_V1_STR}", | |
| tags=["scraping"] | |
| ) | |
| async def root(): | |
| """Root endpoint""" | |
| return { | |
| "message": "Welcome to ERUMESU Scraper API", | |
| "version": "1.0.0", | |
| "docs": "/docs", | |
| "health": "/health" | |
| } | |
| async def health_check(): | |
| """Health check endpoint""" | |
| uptime = time.time() - app_start_time | |
| return HealthResponse( | |
| status="healthy", | |
| version="1.0.0", | |
| uptime=uptime | |
| ) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "app.main:app", | |
| host="0.0.0.0", | |
| port=7860, | |
| reload=False | |
| ) |