mirror of
https://github.com/OpenHands/OpenHands.git
synced 2026-03-22 13:47:19 +08:00
Feat: Better mechanism for attaching middleware (#6365)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user