app.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import warnings
  2. from contextlib import asynccontextmanager
  3. with warnings.catch_warnings():
  4. warnings.simplefilter('ignore')
  5. from fastapi import (
  6. FastAPI,
  7. )
  8. import openhands.agenthub # noqa F401 (we import this to get the agents registered)
  9. from openhands.server.middleware import (
  10. AttachConversationMiddleware,
  11. InMemoryRateLimiter,
  12. LocalhostCORSMiddleware,
  13. NoCacheMiddleware,
  14. RateLimitMiddleware,
  15. )
  16. from openhands.server.routes.conversation import app as conversation_api_router
  17. from openhands.server.routes.feedback import app as feedback_api_router
  18. from openhands.server.routes.files import app as files_api_router
  19. from openhands.server.routes.github import app as github_api_router
  20. from openhands.server.routes.new_conversation import app as new_conversation_api_router
  21. from openhands.server.routes.public import app as public_api_router
  22. from openhands.server.routes.security import app as security_api_router
  23. from openhands.server.routes.settings import app as settings_router
  24. from openhands.server.shared import openhands_config, session_manager
  25. from openhands.utils.import_utils import get_impl
  26. @asynccontextmanager
  27. async def _lifespan(app: FastAPI):
  28. async with session_manager:
  29. yield
  30. app = FastAPI(lifespan=_lifespan)
  31. app.add_middleware(
  32. LocalhostCORSMiddleware,
  33. allow_credentials=True,
  34. allow_methods=['*'],
  35. allow_headers=['*'],
  36. )
  37. app.add_middleware(NoCacheMiddleware)
  38. app.add_middleware(
  39. RateLimitMiddleware, rate_limiter=InMemoryRateLimiter(requests=10, seconds=1)
  40. )
  41. @app.get('/health')
  42. async def health():
  43. return 'OK'
  44. app.include_router(public_api_router)
  45. app.include_router(files_api_router)
  46. app.include_router(security_api_router)
  47. app.include_router(feedback_api_router)
  48. app.include_router(conversation_api_router)
  49. app.include_router(new_conversation_api_router)
  50. app.include_router(settings_router)
  51. app.include_router(github_api_router)
  52. AttachConversationMiddlewareImpl = get_impl(
  53. AttachConversationMiddleware, openhands_config.attach_conversation_middleware_path
  54. )
  55. app.middleware('http')(AttachConversationMiddlewareImpl(app))