Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,8 +15,9 @@ print(f"Current directory: {os.getcwd()}")
|
|
| 15 |
print(f"Files in directory: {os.listdir('.')}")
|
| 16 |
|
| 17 |
try:
|
| 18 |
-
from fastapi import FastAPI, HTTPException
|
| 19 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 20 |
from pydantic import BaseModel
|
| 21 |
import requests
|
| 22 |
import uvicorn
|
|
@@ -104,6 +105,18 @@ def load_service_configs():
|
|
| 104 |
|
| 105 |
load_service_configs()
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# =============================================
|
| 108 |
# API ENDPOINTS
|
| 109 |
# =============================================
|
|
@@ -138,6 +151,16 @@ async def health():
|
|
| 138 |
"timestamp": datetime.now().isoformat()
|
| 139 |
}
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
@app.post("/api/register")
|
| 142 |
async def register_project(request: RegisterRequest):
|
| 143 |
"""Register a new project with its own webhook URL"""
|
|
@@ -152,10 +175,13 @@ async def register_project(request: RegisterRequest):
|
|
| 152 |
# Validate services
|
| 153 |
invalid_services = [s for s in request.services if s not in services_config]
|
| 154 |
if invalid_services:
|
| 155 |
-
return
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
# Create new project
|
| 161 |
projects[project_id] = Project(
|
|
@@ -175,7 +201,7 @@ async def register_project(request: RegisterRequest):
|
|
| 175 |
|
| 176 |
except Exception as e:
|
| 177 |
print(f"β Registration error: {e}")
|
| 178 |
-
|
| 179 |
|
| 180 |
@app.post("/api/update")
|
| 181 |
async def update_status(update: StatusUpdate):
|
|
@@ -192,7 +218,7 @@ async def update_status(update: StatusUpdate):
|
|
| 192 |
# Check if project exists
|
| 193 |
if project_id not in projects:
|
| 194 |
print(f"β Project not found: {project_id}")
|
| 195 |
-
return {"error": "Project not found"}
|
| 196 |
|
| 197 |
project = projects[project_id]
|
| 198 |
project.last_update = datetime.now()
|
|
@@ -208,7 +234,10 @@ async def update_status(update: StatusUpdate):
|
|
| 208 |
print(f" Error: {update.error}")
|
| 209 |
else:
|
| 210 |
print(f"β οΈ Service {service_type} not in project services")
|
| 211 |
-
return
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
# Check if all services are complete
|
| 214 |
all_completed = all(
|
|
@@ -260,7 +289,7 @@ async def update_status(update: StatusUpdate):
|
|
| 260 |
|
| 261 |
except Exception as e:
|
| 262 |
print(f"β Update error: {e}")
|
| 263 |
-
|
| 264 |
|
| 265 |
@app.get("/api/status/{project_id}")
|
| 266 |
async def get_status(project_id: str):
|
|
@@ -327,5 +356,8 @@ print(f"π Active projects: {len(projects)}")
|
|
| 327 |
print(f"π§ Services: {list(services_config.keys())}")
|
| 328 |
print("π Ready to accept requests on port 7860")
|
| 329 |
print("=" * 60)
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
print(f"Files in directory: {os.listdir('.')}")
|
| 16 |
|
| 17 |
try:
|
| 18 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 19 |
from fastapi.middleware.cors import CORSMiddleware
|
| 20 |
+
from fastapi.responses import JSONResponse
|
| 21 |
from pydantic import BaseModel
|
| 22 |
import requests
|
| 23 |
import uvicorn
|
|
|
|
| 105 |
|
| 106 |
load_service_configs()
|
| 107 |
|
| 108 |
+
# =============================================
|
| 109 |
+
# MIDDLEWARE TO LOG ALL REQUESTS
|
| 110 |
+
# =============================================
|
| 111 |
+
|
| 112 |
+
@app.middleware("http")
|
| 113 |
+
async def log_requests(request: Request, call_next):
|
| 114 |
+
"""Log all incoming requests for debugging"""
|
| 115 |
+
print(f"\nπ¨ {request.method} {request.url.path}")
|
| 116 |
+
response = await call_next(request)
|
| 117 |
+
print(f"π¨ Response status: {response.status_code}")
|
| 118 |
+
return response
|
| 119 |
+
|
| 120 |
# =============================================
|
| 121 |
# API ENDPOINTS
|
| 122 |
# =============================================
|
|
|
|
| 151 |
"timestamp": datetime.now().isoformat()
|
| 152 |
}
|
| 153 |
|
| 154 |
+
@app.get("/test")
|
| 155 |
+
async def test():
|
| 156 |
+
"""Simple test endpoint to verify API is working"""
|
| 157 |
+
return {"message": "API is working!", "timestamp": datetime.now().isoformat()}
|
| 158 |
+
|
| 159 |
+
@app.get("/api/test")
|
| 160 |
+
async def api_test():
|
| 161 |
+
"""Test endpoint for /api prefix"""
|
| 162 |
+
return {"message": "API with /api prefix is working!"}
|
| 163 |
+
|
| 164 |
@app.post("/api/register")
|
| 165 |
async def register_project(request: RegisterRequest):
|
| 166 |
"""Register a new project with its own webhook URL"""
|
|
|
|
| 175 |
# Validate services
|
| 176 |
invalid_services = [s for s in request.services if s not in services_config]
|
| 177 |
if invalid_services:
|
| 178 |
+
return JSONResponse(
|
| 179 |
+
status_code=400,
|
| 180 |
+
content={
|
| 181 |
+
"error": f"Unknown services: {invalid_services}",
|
| 182 |
+
"available_services": list(services_config.keys())
|
| 183 |
+
}
|
| 184 |
+
)
|
| 185 |
|
| 186 |
# Create new project
|
| 187 |
projects[project_id] = Project(
|
|
|
|
| 201 |
|
| 202 |
except Exception as e:
|
| 203 |
print(f"β Registration error: {e}")
|
| 204 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 205 |
|
| 206 |
@app.post("/api/update")
|
| 207 |
async def update_status(update: StatusUpdate):
|
|
|
|
| 218 |
# Check if project exists
|
| 219 |
if project_id not in projects:
|
| 220 |
print(f"β Project not found: {project_id}")
|
| 221 |
+
return JSONResponse(status_code=404, content={"error": "Project not found"})
|
| 222 |
|
| 223 |
project = projects[project_id]
|
| 224 |
project.last_update = datetime.now()
|
|
|
|
| 234 |
print(f" Error: {update.error}")
|
| 235 |
else:
|
| 236 |
print(f"β οΈ Service {service_type} not in project services")
|
| 237 |
+
return JSONResponse(
|
| 238 |
+
status_code=400,
|
| 239 |
+
content={"error": f"Service {service_type} not found in project"}
|
| 240 |
+
)
|
| 241 |
|
| 242 |
# Check if all services are complete
|
| 243 |
all_completed = all(
|
|
|
|
| 289 |
|
| 290 |
except Exception as e:
|
| 291 |
print(f"β Update error: {e}")
|
| 292 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 293 |
|
| 294 |
@app.get("/api/status/{project_id}")
|
| 295 |
async def get_status(project_id: str):
|
|
|
|
| 356 |
print(f"π§ Services: {list(services_config.keys())}")
|
| 357 |
print("π Ready to accept requests on port 7860")
|
| 358 |
print("=" * 60)
|
| 359 |
+
print("\nπ‘ Registered routes:")
|
| 360 |
+
for route in app.routes:
|
| 361 |
+
if hasattr(route, "methods") and hasattr(route, "path"):
|
| 362 |
+
print(f" {route.methods} {route.path}")
|
| 363 |
+
print("=" * 60)
|