Add logs limiter

This commit is contained in:
IgorVolochay
2026-09-01 11:04:44 +03:00
parent 86eba8752a
commit 211f3840a0
3 changed files with 48 additions and 20 deletions
+26 -19
View File
@@ -7,6 +7,7 @@ import asyncio
from dotenv import load_dotenv from dotenv import load_dotenv
from fastapi import FastAPI, Depends, Response, Header, HTTPException, status from fastapi import FastAPI, Depends, Response, Header, HTTPException, status
from guard import SecurityMiddleware, SecurityConfig, SecurityDecorator from guard import SecurityMiddleware, SecurityConfig, SecurityDecorator
from contextlib import asynccontextmanager
from typing import Optional from typing import Optional
@@ -17,12 +18,35 @@ from rabbit_worker import RabbitWorker
from tools.base_moderation import moderate_text from tools.base_moderation import moderate_text
from logger import logger, setup_logging from logger import logger, setup_logging
from middleware import RequestLoggingMiddleware from middleware import RequestLoggingMiddleware
from tg_auth import get_current_user_id, DEV_MODE from tg_auth import get_current_user_id
setup_logging() setup_logging()
load_dotenv() load_dotenv()
DEV_MODE: bool = os.getenv("DEV_MODE", "false").lower() == "true"
mongo_worker = MongoWorker()
_rabbit_worker: Optional[RabbitWorker] = None
def get_rabbit_worker() -> RabbitWorker:
global _rabbit_worker
if _rabbit_worker is None:
_rabbit_worker = RabbitWorker()
return _rabbit_worker
@asynccontextmanager
async def lifespan(app: FastAPI):
await mongo_worker.create_indexes()
if DEV_MODE:
logger.warning("⚠️ DEV_MODE is enabled — docs are exposed and Telegram initData auth is DISABLED")
logger.info("Application started on :5000")
yield
mongo_worker.client.close()
logger.info("Application shutdown completed.")
app: FastAPI = FastAPI( app: FastAPI = FastAPI(
title="This OR That", title="This OR That",
summary="OpenAPI schema for \"This OR That\" project!", summary="OpenAPI schema for \"This OR That\" project!",
@@ -31,6 +55,7 @@ app: FastAPI = FastAPI(
docs_url="/docs" if DEV_MODE else None, docs_url="/docs" if DEV_MODE else None,
redoc_url="/redoc" if DEV_MODE else None, redoc_url="/redoc" if DEV_MODE else None,
openapi_url="/openapi.json" if DEV_MODE else None, openapi_url="/openapi.json" if DEV_MODE else None,
lifespan=lifespan,
) )
config = SecurityConfig( config = SecurityConfig(
enable_rate_limiting=True, enable_rate_limiting=True,
@@ -60,15 +85,6 @@ app.add_middleware(SecurityMiddleware, config=config)
app.add_middleware(RequestLoggingMiddleware) app.add_middleware(RequestLoggingMiddleware)
app.state.guard_decorator = guard_deco app.state.guard_decorator = guard_deco
app.state._security_middleware = _security_middleware app.state._security_middleware = _security_middleware
mongo_worker = MongoWorker()
_rabbit_worker: Optional[RabbitWorker] = None
def get_rabbit_worker() -> RabbitWorker:
global _rabbit_worker
if _rabbit_worker is None:
_rabbit_worker = RabbitWorker()
return _rabbit_worker
MODERATION_SECRET = os.getenv("MODERATION_SECRET", "change-me-in-production") MODERATION_SECRET = os.getenv("MODERATION_SECRET", "change-me-in-production")
@@ -83,15 +99,6 @@ async def verify_moderation_secret(
) )
return x_moderation_secret return x_moderation_secret
@app.on_event("startup")
async def startup_event():
await mongo_worker.create_indexes()
if DEV_MODE:
logger.warning("⚠️ DEV_MODE is enabled — docs are exposed and Telegram initData auth is DISABLED")
logger.info("Application started on :5000")
@app.get("/check_user", status_code=200) @app.get("/check_user", status_code=200)
async def check_user( async def check_user(
user_id: int, user_id: int,
+1 -1
View File
@@ -24,7 +24,7 @@ from logger import logger
load_dotenv() load_dotenv()
DEV_MODE: bool = os.getenv("DEV_MODE", "true").lower() == "true" DEV_MODE: bool = os.getenv("DEV_MODE", "false").lower() == "true"
TG_BOT_TOKEN: str = os.getenv("TG_BOT_TOKEN", "") TG_BOT_TOKEN: str = os.getenv("TG_BOT_TOKEN", "")
# Maximum allowed age of initData in seconds (1 hour). # Maximum allowed age of initData in seconds (1 hour).
+21
View File
@@ -10,6 +10,12 @@ services:
- "127.0.0.1:${MONGO_PORT}:27017" - "127.0.0.1:${MONGO_PORT}:27017"
networks: networks:
- tort-net - tort-net
command: mongod --quiet
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "1"
healthcheck: healthcheck:
test: [ "CMD", "mongosh", "--username", "${MONGO_USER}", "--password", "${MONGO_PASS}", "--eval", "db.runCommand({ ping: 1 })" ] test: [ "CMD", "mongosh", "--username", "${MONGO_USER}", "--password", "${MONGO_PASS}", "--eval", "db.runCommand({ ping: 1 })" ]
interval: 10s interval: 10s
@@ -28,6 +34,11 @@ services:
- "127.0.0.1:15672:15672" - "127.0.0.1:15672:15672"
networks: networks:
- tort-net - tort-net
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "1"
healthcheck: healthcheck:
test: [ "CMD", "rabbitmq-diagnostics", "-q", "ping" ] test: [ "CMD", "rabbitmq-diagnostics", "-q", "ping" ]
interval: 10s interval: 10s
@@ -58,6 +69,11 @@ services:
MODERATION_SECRET: ${MODERATION_SECRET} MODERATION_SECRET: ${MODERATION_SECRET}
DEV_MODE: ${DEV_MODE:-false} DEV_MODE: ${DEV_MODE:-false}
LOG_LEVEL: ${LOG_LEVEL:-INFO} LOG_LEVEL: ${LOG_LEVEL:-INFO}
logging:
driver: "json-file"
options:
max-size: "50m"
max-file: "2"
ports: ports:
- "127.0.0.1:5000:5000" - "127.0.0.1:5000:5000"
networks: networks:
@@ -85,6 +101,11 @@ services:
RABBIT_USER: ${RABBIT_USER} RABBIT_USER: ${RABBIT_USER}
RABBIT_PASS: ${RABBIT_PASS} RABBIT_PASS: ${RABBIT_PASS}
LOG_LEVEL: ${LOG_LEVEL:-INFO} LOG_LEVEL: ${LOG_LEVEL:-INFO}
logging:
driver: "json-file"
options:
max-size: "50m"
max-file: "1"
networks: networks:
- tort-net - tort-net