Commit ·
cbcbcd3
1
Parent(s): 29ce62f
added test file
Browse files- behavior_backend/app/api/routes/health.py +20 -4
- behavior_backend/main.py +4 -2
- test_api.py +38 -0
behavior_backend/app/api/routes/health.py
CHANGED
|
@@ -12,16 +12,32 @@ router = APIRouter(
|
|
| 12 |
)
|
| 13 |
|
| 14 |
@router.get("")
|
| 15 |
-
async def health_check(
|
| 16 |
"""
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
Checks database connectivity.
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
# Try to execute a simple query to check DB connection
|
| 22 |
db.execute("SELECT 1")
|
| 23 |
logger.info("Health check: Database connection successful.")
|
| 24 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
logger.error(f"Health check: Database connection failed - {str(e)}")
|
| 27 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
@router.get("")
|
| 15 |
+
async def health_check():
|
| 16 |
"""
|
| 17 |
+
Simple health check endpoint that doesn't require authentication
|
| 18 |
+
"""
|
| 19 |
+
return {"status": "ok"}
|
| 20 |
+
|
| 21 |
+
@router.get("/api")
|
| 22 |
+
async def api_health_check(db: Session = Depends(get_db)):
|
| 23 |
+
"""
|
| 24 |
+
Detailed health check of the API and its dependencies.
|
| 25 |
Checks database connectivity.
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
# Try to execute a simple query to check DB connection
|
| 29 |
db.execute("SELECT 1")
|
| 30 |
logger.info("Health check: Database connection successful.")
|
| 31 |
+
return {
|
| 32 |
+
"status": "healthy",
|
| 33 |
+
"database": "connected",
|
| 34 |
+
"api": "running"
|
| 35 |
+
}
|
| 36 |
except Exception as e:
|
| 37 |
logger.error(f"Health check: Database connection failed - {str(e)}")
|
| 38 |
+
return {
|
| 39 |
+
"status": "unhealthy",
|
| 40 |
+
"database": "disconnected",
|
| 41 |
+
"api": "running",
|
| 42 |
+
"error": str(e)
|
| 43 |
+
}
|
behavior_backend/main.py
CHANGED
|
@@ -84,16 +84,18 @@ app.mount("/uploads", StaticFiles(directory=str(settings.UPLOAD_DIR)), name="upl
|
|
| 84 |
# Create main API router
|
| 85 |
api_router = APIRouter()
|
| 86 |
|
| 87 |
-
# Include all routers
|
| 88 |
api_router.include_router(videos_router)
|
| 89 |
api_router.include_router(processing_router)
|
| 90 |
api_router.include_router(users_router)
|
| 91 |
api_router.include_router(auth_router)
|
| 92 |
-
api_router.include_router(health_router)
|
| 93 |
|
| 94 |
# Include API router in app
|
| 95 |
app.include_router(api_router, prefix=settings.API_V1_STR)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
| 97 |
@app.middleware("http")
|
| 98 |
async def log_requests(request: Request, call_next):
|
| 99 |
"""Log all requests and their processing time"""
|
|
|
|
| 84 |
# Create main API router
|
| 85 |
api_router = APIRouter()
|
| 86 |
|
| 87 |
+
# Include all routers except health
|
| 88 |
api_router.include_router(videos_router)
|
| 89 |
api_router.include_router(processing_router)
|
| 90 |
api_router.include_router(users_router)
|
| 91 |
api_router.include_router(auth_router)
|
|
|
|
| 92 |
|
| 93 |
# Include API router in app
|
| 94 |
app.include_router(api_router, prefix=settings.API_V1_STR)
|
| 95 |
|
| 96 |
+
# Mount health router directly to app
|
| 97 |
+
app.include_router(health_router)
|
| 98 |
+
|
| 99 |
@app.middleware("http")
|
| 100 |
async def log_requests(request: Request, call_next):
|
| 101 |
"""Log all requests and their processing time"""
|
test_api.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
# Base URL of your Space
|
| 5 |
+
BASE_URL = "https://urjob-test.hf.space"
|
| 6 |
+
|
| 7 |
+
# The JWT token from your logs
|
| 8 |
+
HF_SIGN = "eyJhbGciOiJFZERTQSJ9.eyJyZWFkIjp0cnVlLCJwZXJtaXNzaW9ucyI6eyJyZXBvLmNvbnRlbnQucmVhZCI6dHJ1ZX0sIm9uQmVoYWxmT2YiOnsia2luZCI6InVzZXIiLCJfaWQiOiI2N2FiYzRmMDQxMjM1ODI0NDQyNDQwNmUiLCJ1c2VyIjoiaGliYXRvcnJhaG1lbiIsInNlc3Npb25JZCI6IjY4MmM0MjUyZTE3MzJjNzZiNGM1MjdlOCJ9LCJpYXQiOjE3NDc5NTMxODMsInN1YiI6Ii9zcGFjZXMvdXJqb2IvdGVzdCIsImV4cCI6MTc0ODAzOTU4MywiaXNzIjoiaHR0cHM6Ly9odWdnaW5nZmFjZS5jbyJ9.9Ukimz1w0j8hAohN7HxGbadMPD0FZpDMYFuSHTxFByOA8HtMG3U-HuZNs2sSo3reqnb3gkKgZx0d_99VaWmFAQ"
|
| 9 |
+
|
| 10 |
+
def test_root():
|
| 11 |
+
"""Test the root endpoint with HF Space sign"""
|
| 12 |
+
params = {
|
| 13 |
+
"__sign": HF_SIGN
|
| 14 |
+
}
|
| 15 |
+
response = requests.get(f"{BASE_URL}/", params=params)
|
| 16 |
+
print("\nRoot Endpoint (/) Response:", response.status_code)
|
| 17 |
+
print(response.json() if response.ok else response.text)
|
| 18 |
+
|
| 19 |
+
def test_basic_health():
|
| 20 |
+
"""Test the basic health endpoint"""
|
| 21 |
+
response = requests.get(f"{BASE_URL}/health")
|
| 22 |
+
print("\nBasic Health Endpoint (/health) Response:", response.status_code)
|
| 23 |
+
print(response.json() if response.ok else response.text)
|
| 24 |
+
|
| 25 |
+
def test_api_health():
|
| 26 |
+
"""Test the API health endpoint with HF Space sign"""
|
| 27 |
+
params = {
|
| 28 |
+
"__sign": HF_SIGN
|
| 29 |
+
}
|
| 30 |
+
response = requests.get(f"{BASE_URL}/health/api", params=params)
|
| 31 |
+
print("\nAPI Health Endpoint (/health/api) Response:", response.status_code)
|
| 32 |
+
print(response.json() if response.ok else response.text)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
print("Testing API endpoints...")
|
| 36 |
+
test_root()
|
| 37 |
+
test_basic_health()
|
| 38 |
+
test_api_health()
|