Feat: Better mechanism for attaching middleware (#6365)

This commit is contained in:
tofarr
2025-01-23 07:31:43 -07:00
committed by GitHub
parent aa223734d4
commit 5ba9a6d321
4 changed files with 37 additions and 32 deletions

View File

@@ -1,8 +1,15 @@
import os
from fastapi import HTTPException
from fastapi import FastAPI, HTTPException
from openhands.core.logger import openhands_logger as logger
from openhands.server.middleware import (
AttachConversationMiddleware,
CacheControlMiddleware,
InMemoryRateLimiter,
LocalhostCORSMiddleware,
RateLimitMiddleware,
)
from openhands.server.types import AppMode, OpenhandsConfigInterface
from openhands.utils.import_utils import get_impl
@@ -12,9 +19,6 @@ class OpenhandsConfig(OpenhandsConfigInterface):
app_mode = AppMode.OSS
posthog_client_key = 'phc_3ESMmY9SgqEAGBB6sMGK5ayYHkeUuknH2vP6FmWH9RA'
github_client_id = os.environ.get('GITHUB_APP_CLIENT_ID', '')
attach_conversation_middleware_path = (
'openhands.server.middleware.AttachConversationMiddleware'
)
settings_store_class: str = (
'openhands.storage.settings.file_settings_store.FileSettingsStore'
)
@@ -42,6 +46,21 @@ class OpenhandsConfig(OpenhandsConfigInterface):
return config
def attach_middleware(self, api: FastAPI) -> None:
api.add_middleware(
LocalhostCORSMiddleware,
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
api.add_middleware(CacheControlMiddleware)
api.add_middleware(
RateLimitMiddleware,
rate_limiter=InMemoryRateLimiter(requests=10, seconds=1),
)
api.middleware('http')(AttachConversationMiddleware(api))
def load_openhands_config():
config_cls = os.environ.get('OPENHANDS_CONFIG_CLS', None)