Spaces:
Sleeping
Sleeping
Claude Code commited on
Commit ·
2417977
1
Parent(s): 2271dd3
Claude Code: Fix app startup - use modern lifespan context manager instead of deprecated on_event decorators
Browse files
app.py
CHANGED
|
@@ -19,6 +19,7 @@ def get_app():
|
|
| 19 |
"""Create FastAPI app with imports inside function."""
|
| 20 |
from fastapi import FastAPI, Request, HTTPException
|
| 21 |
from fastapi.responses import JSONResponse
|
|
|
|
| 22 |
import logging
|
| 23 |
|
| 24 |
print(">>> CAIN: Creating FastAPI app...", flush=True)
|
|
@@ -32,10 +33,19 @@ def get_app():
|
|
| 32 |
logger = logging.getLogger(__name__)
|
| 33 |
logger.info(">>> CAIN: FastAPI initialization starting")
|
| 34 |
|
| 35 |
-
app = FastAPI(title="HuggingClaw - Cain", version="0.0.1")
|
| 36 |
-
|
| 37 |
START_TIME = time.time()
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
@app.get("/")
|
| 40 |
async def root():
|
| 41 |
"""Root endpoint - simple alive status."""
|
|
@@ -56,18 +66,6 @@ def get_app():
|
|
| 56 |
content={"error": True, "message": str(exc), "type": type(exc).__name__}
|
| 57 |
)
|
| 58 |
|
| 59 |
-
@app.on_event("startup")
|
| 60 |
-
async def startup():
|
| 61 |
-
"""Log startup complete."""
|
| 62 |
-
print(">>> CAIN: Startup complete - ready to serve", flush=True)
|
| 63 |
-
logger.info(">>> CAIN: Ready to serve requests on port %s", os.environ.get('PORT', '7860'))
|
| 64 |
-
|
| 65 |
-
@app.on_event("shutdown")
|
| 66 |
-
async def shutdown():
|
| 67 |
-
"""Log shutdown."""
|
| 68 |
-
print(">>> CAIN: Shutdown triggered", flush=True)
|
| 69 |
-
logger.info(">>> CAIN: Shutting down")
|
| 70 |
-
|
| 71 |
print(">>> CAIN: FastAPI app created successfully", flush=True)
|
| 72 |
return app
|
| 73 |
|
|
|
|
| 19 |
"""Create FastAPI app with imports inside function."""
|
| 20 |
from fastapi import FastAPI, Request, HTTPException
|
| 21 |
from fastapi.responses import JSONResponse
|
| 22 |
+
from contextlib import asynccontextmanager
|
| 23 |
import logging
|
| 24 |
|
| 25 |
print(">>> CAIN: Creating FastAPI app...", flush=True)
|
|
|
|
| 33 |
logger = logging.getLogger(__name__)
|
| 34 |
logger.info(">>> CAIN: FastAPI initialization starting")
|
| 35 |
|
|
|
|
|
|
|
| 36 |
START_TIME = time.time()
|
| 37 |
|
| 38 |
+
@asynccontextmanager
|
| 39 |
+
async def lifespan(app: FastAPI):
|
| 40 |
+
"""Lifespan context manager for startup/shutdown events."""
|
| 41 |
+
print(">>> CAIN: Startup complete - ready to serve", flush=True)
|
| 42 |
+
logger.info(">>> CAIN: Ready to serve requests on port %s", os.environ.get('PORT', '7860'))
|
| 43 |
+
yield
|
| 44 |
+
print(">>> CAIN: Shutdown triggered", flush=True)
|
| 45 |
+
logger.info(">>> CAIN: Shutting down")
|
| 46 |
+
|
| 47 |
+
app = FastAPI(title="HuggingClaw - Cain", version="0.0.1", lifespan=lifespan)
|
| 48 |
+
|
| 49 |
@app.get("/")
|
| 50 |
async def root():
|
| 51 |
"""Root endpoint - simple alive status."""
|
|
|
|
| 66 |
content={"error": True, "message": str(exc), "type": type(exc).__name__}
|
| 67 |
)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
print(">>> CAIN: FastAPI app created successfully", flush=True)
|
| 70 |
return app
|
| 71 |
|