Commit ·
d7ff81e
1
Parent(s): 8d29e78
fixed main
Browse files- behavior_backend/main.py +31 -4
behavior_backend/main.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
import uvicorn
|
| 2 |
-
from fastapi import FastAPI, APIRouter
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
import asyncio
|
| 6 |
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from app.core.config import settings
|
| 9 |
from app.api.routes.videos import router as videos_router
|
|
@@ -16,11 +19,15 @@ from app.db.base import create_tables
|
|
| 16 |
# Create database tables
|
| 17 |
create_tables()
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Create FastAPI app
|
| 20 |
app = FastAPI(
|
| 21 |
-
title=
|
| 22 |
-
description=
|
| 23 |
-
version=
|
| 24 |
)
|
| 25 |
|
| 26 |
# Configure CORS
|
|
@@ -50,6 +57,26 @@ api_router.include_router(health_router)
|
|
| 50 |
# Include API router in app
|
| 51 |
app.include_router(api_router, prefix=settings.API_V1_STR)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
@app.on_event("shutdown")
|
| 54 |
async def shutdown_event():
|
| 55 |
loop = asyncio.get_running_loop()
|
|
|
|
| 1 |
import uvicorn
|
| 2 |
+
from fastapi import FastAPI, APIRouter, Request
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
import asyncio
|
| 6 |
from concurrent.futures import ThreadPoolExecutor
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
+
import logging
|
| 9 |
+
import time
|
| 10 |
|
| 11 |
from app.core.config import settings
|
| 12 |
from app.api.routes.videos import router as videos_router
|
|
|
|
| 19 |
# Create database tables
|
| 20 |
create_tables()
|
| 21 |
|
| 22 |
+
# Configure logging
|
| 23 |
+
logging.basicConfig(level=logging.INFO)
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
# Create FastAPI app
|
| 27 |
app = FastAPI(
|
| 28 |
+
title="Behavior Analytics API",
|
| 29 |
+
description="API for behavior analytics processing",
|
| 30 |
+
version="1.0.0"
|
| 31 |
)
|
| 32 |
|
| 33 |
# Configure CORS
|
|
|
|
| 57 |
# Include API router in app
|
| 58 |
app.include_router(api_router, prefix=settings.API_V1_STR)
|
| 59 |
|
| 60 |
+
@app.middleware("http")
|
| 61 |
+
async def log_requests(request: Request, call_next):
|
| 62 |
+
"""Log all requests and their processing time"""
|
| 63 |
+
start_time = time.time()
|
| 64 |
+
response = await call_next(request)
|
| 65 |
+
process_time = time.time() - start_time
|
| 66 |
+
logger.info(f"Path: {request.url.path} Method: {request.method} Time: {process_time:.2f}s Status: {response.status_code}")
|
| 67 |
+
return response
|
| 68 |
+
|
| 69 |
+
@app.get("/")
|
| 70 |
+
async def root():
|
| 71 |
+
"""Root endpoint that returns API status"""
|
| 72 |
+
return JSONResponse({
|
| 73 |
+
"status": "ok",
|
| 74 |
+
"message": "Behavior Analytics API is running",
|
| 75 |
+
"version": "1.0.0",
|
| 76 |
+
"docs_url": "/docs", # Link to API documentation
|
| 77 |
+
"openapi_url": "/openapi.json" # Link to OpenAPI schema
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
@app.on_event("shutdown")
|
| 81 |
async def shutdown_event():
|
| 82 |
loop = asyncio.get_running_loop()
|