CosmicShareMain / app.py
garvitcpp's picture
Update app.py
7e267d3 verified
raw
history blame
1.52 kB
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)
@app.on_event("startup")
async def startup_event():
scheduler.start()
@app.on_event("shutdown")
async def shutdown_event():
scheduler.shutdown()
@app.get("/")
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)