Spaces:
Running
Running
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from apscheduler.schedulers.asyncio import AsyncIOScheduler | |
| import uvicorn | |
| def create_app(): | |
| app = FastAPI( | |
| title="CosmicShare API", | |
| docs_url="/docs", | |
| redoc_url="/redoc" | |
| ) | |
| # Configure CORS for Hugging Face Spaces | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Be more restrictive in production | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Import routes after FastAPI instance is created | |
| from app.routes import file_routes | |
| from app.services.storage_service import StorageService | |
| # Initialize services | |
| storage_service = StorageService() | |
| # Register routes | |
| app.include_router(file_routes.router) # Remove prefix for Hugging Face | |
| # Set up scheduler | |
| scheduler = AsyncIOScheduler() | |
| scheduler.add_job(storage_service.cleanup_expired_files, 'interval', minutes=1) | |
| async def startup_event(): | |
| scheduler.start() | |
| async def shutdown_event(): | |
| scheduler.shutdown() | |
| async def root(): | |
| return { | |
| "message": "CosmicShare API is running", | |
| "documentation": "/docs", | |
| "upload_endpoint": "/upload" | |
| } | |
| return app | |
| app = create_app() | |
| if __name__ == "__main__": | |
| uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True) |