-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from fedecarboni7/monitoring-and-ai
Monitoring and ai
- Loading branch information
Showing
12 changed files
with
200 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,56 @@ | ||
import os | ||
import time | ||
|
||
from fastapi import FastAPI, Request | ||
from fastapi.responses import RedirectResponse | ||
from fastapi import Depends, FastAPI, Request | ||
from fastapi.openapi.docs import get_swagger_ui_html | ||
from fastapi.responses import JSONResponse, RedirectResponse | ||
from fastapi.staticfiles import StaticFiles | ||
from fastapi.templating import Jinja2Templates | ||
from starlette.exceptions import HTTPException as StarletteHTTPException | ||
from starlette.middleware.sessions import SessionMiddleware | ||
|
||
from app.config.logging_config import configure_logging, logger | ||
from app.config.logging_config import configure_logging | ||
from app.db.models import User | ||
from app.utils.auth import get_current_user | ||
from app.utils.security import verify_admin_user | ||
|
||
templates = Jinja2Templates(directory="templates") | ||
|
||
def create_app() -> FastAPI: | ||
app = FastAPI(docs_url=None, redoc_url=None) | ||
|
||
# Configuración de logging | ||
configure_logging() | ||
|
||
secret_key = os.getenv("SECRET_KEY") | ||
|
||
app = FastAPI(title="Armar Equipos", docs_url=None, redoc_url=None, openapi_url=None) | ||
|
||
app.add_middleware(SessionMiddleware, secret_key=secret_key) | ||
|
||
app.mount("/static", StaticFiles(directory="static"), name="static") | ||
|
||
@app.middleware("http") | ||
async def measure_execution_time(request: Request, call_next): | ||
ignore_paths = ["/static", "/favicon.ico", "/sm/"] | ||
|
||
if not any(request.url.path.startswith(prefix) for prefix in ignore_paths): | ||
start_time = time.time() | ||
response = await call_next(request) | ||
process_time = time.time() - start_time | ||
logger.debug(f"{process_time:.4f} seconds to process request: {request.method} {request.url.path}") | ||
return response | ||
else: | ||
return await call_next(request) | ||
|
||
@app.exception_handler(500) | ||
async def internal_server_error_handler(request: Request, exc: Exception): | ||
""" | ||
Maneja los errores del servidor y muestra una página de error personalizada. | ||
""" | ||
return templates.TemplateResponse(request=request, name="500.html", status_code=500) | ||
|
||
|
||
@app.exception_handler(StarletteHTTPException) | ||
async def http_exception_handler(request: Request, exc: StarletteHTTPException): | ||
if exc.status_code in [404, 405]: # Not Found y Method Not Allowed | ||
return RedirectResponse(url="/") | ||
raise exc | ||
|
||
if exc.status_code == 500: | ||
return templates.TemplateResponse(request=request, name="500.html", status_code=500) | ||
|
||
return JSONResponse( | ||
status_code=exc.status_code, | ||
content={ | ||
"error": exc.status_code, | ||
"detail": exc.detail | ||
} | ||
) | ||
|
||
@app.get("/docs", include_in_schema=False) | ||
async def get_documentation(current_user: User = Depends(get_current_user)): | ||
verify_admin_user(current_user, detail="Unauthorized: /docs") | ||
return get_swagger_ui_html(openapi_url="/openapi.json", title="docs") | ||
|
||
@app.get("/openapi.json", include_in_schema=False) | ||
async def openapi(current_user: User = Depends(get_current_user)): | ||
verify_admin_user(current_user, detail="Unauthorized: /openapi.json") | ||
return app.openapi() | ||
|
||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.