mirror of
https://github.com/Viren070/MediaFusion.git
synced 2025-12-01 23:21:11 +01:00
Setup Secure server logging & disable uvicorn access logs
This commit is contained in:
@@ -1 +1 @@
|
||||
web: uvicorn --host 0.0.0.0 --port $PORT api.main:app
|
||||
web: uvicorn --no-access-log --host 0.0.0.0 --port $PORT api.main:app
|
||||
@@ -18,6 +18,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import RedirectResponse, FileResponse, StreamingResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from api.middleware import SecureLoggingMiddleware
|
||||
from db import database, crud, schemas
|
||||
from db.config import settings
|
||||
from scrappers import tamil_blasters, tamilmv
|
||||
@@ -65,6 +66,11 @@ cache = Cache(
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def add_custom_logging(request: Request, call_next):
|
||||
return await SecureLoggingMiddleware()(request, call_next)
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def init_server():
|
||||
await database.init()
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import logging
|
||||
|
||||
from fastapi.requests import Request
|
||||
from fastapi.responses import Response
|
||||
|
||||
|
||||
class SecureLoggingMiddleware:
|
||||
async def __call__(self, request: Request, call_next):
|
||||
response = await call_next(request)
|
||||
self.custom_log(request, response)
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def custom_log(request: Request, response: Response):
|
||||
url_path = request.url.path
|
||||
if request.path_params.get("secret_str"):
|
||||
url_path = url_path.replace(
|
||||
request.path_params.get("secret_str"), "***MASKED***"
|
||||
)
|
||||
logging.info(
|
||||
f'{request.client.host} - "{request.method} {url_path} HTTP/1.1" {response.status_code}'
|
||||
)
|
||||
@@ -18,4 +18,4 @@ COPY .. .
|
||||
# Expose the port
|
||||
EXPOSE 80
|
||||
|
||||
CMD ["pipenv", "run", "uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "80"]
|
||||
CMD ["pipenv", "run", "uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "80", "--no-access-log"]
|
||||
|
||||
Reference in New Issue
Block a user