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

@@ -10,13 +10,6 @@ from fastapi import (
import openhands.agenthub # noqa F401 (we import this to get the agents registered)
from openhands import __version__
from openhands.server.middleware import (
AttachConversationMiddleware,
CacheControlMiddleware,
InMemoryRateLimiter,
LocalhostCORSMiddleware,
RateLimitMiddleware,
)
from openhands.server.routes.conversation import app as conversation_api_router
from openhands.server.routes.feedback import app as feedback_api_router
from openhands.server.routes.files import app as files_api_router
@@ -29,7 +22,6 @@ from openhands.server.routes.security import app as security_api_router
from openhands.server.routes.settings import app as settings_router
from openhands.server.routes.trajectory import app as trajectory_router
from openhands.server.shared import openhands_config, session_manager
from openhands.utils.import_utils import get_impl
@asynccontextmanager
@@ -44,17 +36,7 @@ app = FastAPI(
version=__version__,
lifespan=_lifespan,
)
app.add_middleware(
LocalhostCORSMiddleware,
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
app.add_middleware(CacheControlMiddleware)
app.add_middleware(
RateLimitMiddleware, rate_limiter=InMemoryRateLimiter(requests=10, seconds=1)
)
openhands_config.attach_middleware(app)
@app.get('/health')
@@ -71,8 +53,3 @@ app.include_router(manage_conversation_api_router)
app.include_router(settings_router)
app.include_router(github_api_router)
app.include_router(trajectory_router)
AttachConversationMiddlewareImpl = get_impl(
AttachConversationMiddleware, openhands_config.attach_conversation_middleware_path
)
app.middleware('http')(AttachConversationMiddlewareImpl(app))