feat(platform): add audit logs and activity feed
This commit is contained in:
parent
92cb8d1286
commit
c816e9869d
34 changed files with 1592 additions and 43 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import logging
|
||||
import time
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import HTTPException
|
||||
|
|
@ -9,6 +10,7 @@ from starlette import status
|
|||
from starlette.requests import Request
|
||||
|
||||
from app.api.auth import router as auth_router
|
||||
from app.api.audit import router as audit_router
|
||||
from app.api.customers import router as customers_router
|
||||
from app.api.dashboard import router as dashboard_router
|
||||
from app.api.permissions import router as permissions_router
|
||||
|
|
@ -16,8 +18,11 @@ from app.api.roles import router as roles_router
|
|||
from app.api.users import router as users_router
|
||||
from app.db.database import SessionLocal
|
||||
from app.db.health import check_database
|
||||
from app.core.logging import configure_logging
|
||||
from app.rbac.seed import seed_rbac
|
||||
|
||||
configure_logging()
|
||||
|
||||
app = FastAPI(
|
||||
title="Hermes API",
|
||||
version="0.1.0",
|
||||
|
|
@ -25,6 +30,7 @@ app = FastAPI(
|
|||
)
|
||||
|
||||
app.include_router(auth_router)
|
||||
app.include_router(audit_router)
|
||||
app.include_router(users_router)
|
||||
app.include_router(roles_router)
|
||||
app.include_router(permissions_router)
|
||||
|
|
@ -34,6 +40,34 @@ app.include_router(dashboard_router)
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_logging_middleware(request: Request, call_next):
|
||||
start = time.perf_counter()
|
||||
response = await call_next(request)
|
||||
duration_ms = round((time.perf_counter() - start) * 1000, 2)
|
||||
logger.info(
|
||||
"request.completed",
|
||||
extra={
|
||||
"method": request.method,
|
||||
"path": request.url.path,
|
||||
"status_code": response.status_code,
|
||||
"duration_ms": duration_ms,
|
||||
},
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
def error_code_for_status(status_code: int) -> str:
|
||||
return {
|
||||
400: "BAD_REQUEST",
|
||||
401: "UNAUTHORIZED",
|
||||
403: "FORBIDDEN",
|
||||
404: "NOT_FOUND",
|
||||
409: "CONFLICT",
|
||||
422: "VALIDATION_ERROR",
|
||||
}.get(status_code, "HTTP_ERROR")
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
def startup_seed_rbac():
|
||||
db = SessionLocal()
|
||||
|
|
@ -55,7 +89,13 @@ async def http_exception_handler(request: Request, exc: HTTPException):
|
|||
)
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={"detail": exc.detail},
|
||||
content={
|
||||
"success": False,
|
||||
"message": str(exc.detail),
|
||||
"error_code": error_code_for_status(exc.status_code),
|
||||
"details": [],
|
||||
"detail": exc.detail,
|
||||
},
|
||||
headers=exc.headers,
|
||||
)
|
||||
|
||||
|
|
@ -68,7 +108,13 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
|
|||
)
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
content={"detail": jsonable_encoder(exc.errors())},
|
||||
content={
|
||||
"success": False,
|
||||
"message": "Validierung fehlgeschlagen",
|
||||
"error_code": "VALIDATION_ERROR",
|
||||
"details": jsonable_encoder(exc.errors()),
|
||||
"detail": jsonable_encoder(exc.errors()),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -80,7 +126,13 @@ async def unhandled_exception_handler(request: Request, exc: Exception):
|
|||
)
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
content={"detail": "Interner Serverfehler"},
|
||||
content={
|
||||
"success": False,
|
||||
"message": "Interner Serverfehler",
|
||||
"error_code": "INTERNAL_SERVER_ERROR",
|
||||
"details": [],
|
||||
"detail": "Interner Serverfehler",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue