Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,57 @@
|
|
| 1 |
-
from
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
| 4 |
+
import uvicorn
|
| 5 |
+
|
| 6 |
+
def create_app():
|
| 7 |
+
app = FastAPI(
|
| 8 |
+
title="CosmicShare API",
|
| 9 |
+
docs_url="/docs",
|
| 10 |
+
redoc_url="/redoc"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Configure CORS for Hugging Face Spaces
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"], # Be more restrictive in production
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Import routes after FastAPI instance is created
|
| 23 |
+
from app.routes import file_routes
|
| 24 |
+
from app.services.storage_service import StorageService
|
| 25 |
+
|
| 26 |
+
# Initialize services
|
| 27 |
+
storage_service = StorageService()
|
| 28 |
+
|
| 29 |
+
# Register routes
|
| 30 |
+
app.include_router(file_routes.router) # Remove prefix for Hugging Face
|
| 31 |
+
|
| 32 |
+
# Set up scheduler
|
| 33 |
+
scheduler = AsyncIOScheduler()
|
| 34 |
+
scheduler.add_job(storage_service.cleanup_expired_files, 'interval', minutes=1)
|
| 35 |
+
|
| 36 |
+
@app.on_event("startup")
|
| 37 |
+
async def startup_event():
|
| 38 |
+
scheduler.start()
|
| 39 |
+
|
| 40 |
+
@app.on_event("shutdown")
|
| 41 |
+
async def shutdown_event():
|
| 42 |
+
scheduler.shutdown()
|
| 43 |
+
|
| 44 |
+
@app.get("/")
|
| 45 |
+
async def root():
|
| 46 |
+
return {
|
| 47 |
+
"message": "CosmicShare API is running",
|
| 48 |
+
"documentation": "/docs",
|
| 49 |
+
"upload_endpoint": "/upload"
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
return app
|
| 53 |
+
|
| 54 |
+
app = create_app()
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|