mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-06 08:07:22 +08:00
refactor: clean up whitespace and improve readability in logging middleware
This commit is contained in:
@@ -5,11 +5,18 @@ from services.logging import LogService
|
|||||||
from models.database import UserAccount
|
from models.database import UserAccount
|
||||||
import jwt
|
import jwt
|
||||||
from jwt.exceptions import InvalidTokenError
|
from jwt.exceptions import InvalidTokenError
|
||||||
from services.auth import ALGORITHM
|
from services.auth import ALGORITHM
|
||||||
from services.config import ConfigCenter
|
from services.config import ConfigCenter
|
||||||
|
|
||||||
|
|
||||||
class LoggingMiddleware(BaseHTTPMiddleware):
|
class LoggingMiddleware(BaseHTTPMiddleware):
|
||||||
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
|
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
|
||||||
|
path = request.url.path
|
||||||
|
method = request.method.upper()
|
||||||
|
if method == "GET":
|
||||||
|
if path == "/api/logs" or path == "/api/plugins" or path.startswith("/api/config"):
|
||||||
|
return await call_next(request)
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
user_id = None
|
user_id = None
|
||||||
if "authorization" in request.headers:
|
if "authorization" in request.headers:
|
||||||
@@ -27,9 +34,9 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
response = await call_next(request)
|
response = await call_next(request)
|
||||||
|
|
||||||
process_time = (time.time() - start_time) * 1000
|
process_time = (time.time() - start_time) * 1000
|
||||||
|
|
||||||
details = {
|
details = {
|
||||||
"client_ip": request.client.host,
|
"client_ip": request.client.host,
|
||||||
"method": request.method,
|
"method": request.method,
|
||||||
@@ -38,9 +45,9 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
|||||||
"status_code": response.status_code,
|
"status_code": response.status_code,
|
||||||
"process_time_ms": round(process_time, 2)
|
"process_time_ms": round(process_time, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
message = f"{request.method} {request.url.path} - {response.status_code}"
|
message = f"{request.method} {request.url.path} - {response.status_code}"
|
||||||
|
|
||||||
await LogService.api(message, details, user_id)
|
await LogService.api(message, details, user_id)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|||||||
Reference in New Issue
Block a user