mirror of
https://github.com/OpenHands/OpenHands.git
synced 2026-03-22 13:47:19 +08:00
Refactor: move middleware definition (#6552)
This commit is contained in:
@@ -1,79 +0,0 @@
|
||||
import os
|
||||
|
||||
from fastapi import FastAPI, HTTPException
|
||||
|
||||
from openhands.core.logger import openhands_logger as logger
|
||||
from openhands.server.middleware import (
|
||||
AttachConversationMiddleware,
|
||||
CacheControlMiddleware,
|
||||
GitHubTokenMiddleware,
|
||||
InMemoryRateLimiter,
|
||||
LocalhostCORSMiddleware,
|
||||
RateLimitMiddleware,
|
||||
)
|
||||
from openhands.server.types import AppMode, OpenhandsConfigInterface
|
||||
from openhands.storage.settings.settings_store import SettingsStore
|
||||
from openhands.utils.import_utils import get_impl
|
||||
|
||||
|
||||
class OpenhandsConfig(OpenhandsConfigInterface):
|
||||
config_cls = os.environ.get('OPENHANDS_CONFIG_CLS', None)
|
||||
app_mode = AppMode.OSS
|
||||
posthog_client_key = 'phc_3ESMmY9SgqEAGBB6sMGK5ayYHkeUuknH2vP6FmWH9RA'
|
||||
github_client_id = os.environ.get('GITHUB_APP_CLIENT_ID', '')
|
||||
settings_store_class: str = (
|
||||
'openhands.storage.settings.file_settings_store.FileSettingsStore'
|
||||
)
|
||||
conversation_store_class: str = (
|
||||
'openhands.storage.conversation.file_conversation_store.FileConversationStore'
|
||||
)
|
||||
conversation_manager_class: str = 'openhands.server.conversation_manager.standalone_conversation_manager.StandaloneConversationManager'
|
||||
|
||||
def verify_config(self):
|
||||
if self.config_cls:
|
||||
raise ValueError('Unexpected config path provided')
|
||||
|
||||
def verify_github_repo_list(self, installation_id: int | None):
|
||||
if self.app_mode == AppMode.OSS and installation_id:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail='Unexpected installation ID',
|
||||
)
|
||||
|
||||
def get_config(self):
|
||||
config = {
|
||||
'APP_MODE': self.app_mode,
|
||||
'GITHUB_CLIENT_ID': self.github_client_id,
|
||||
'POSTHOG_CLIENT_KEY': self.posthog_client_key,
|
||||
}
|
||||
|
||||
return config
|
||||
|
||||
def attach_middleware(self, api: FastAPI) -> None:
|
||||
SettingsStoreImpl = get_impl(SettingsStore, self.settings_store_class) # type: ignore
|
||||
|
||||
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))
|
||||
api.middleware('http')(GitHubTokenMiddleware(api, SettingsStoreImpl)) # type: ignore
|
||||
|
||||
|
||||
def load_openhands_config():
|
||||
config_cls = os.environ.get('OPENHANDS_CONFIG_CLS', None)
|
||||
logger.info(f'Using config class {config_cls}')
|
||||
|
||||
openhands_config_cls = get_impl(OpenhandsConfig, config_cls)
|
||||
openhands_config = openhands_config_cls()
|
||||
openhands_config.verify_config()
|
||||
|
||||
return openhands_config
|
||||
43
openhands/server/config/server_config.py
Normal file
43
openhands/server/config/server_config.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import os
|
||||
|
||||
from openhands.core.logger import openhands_logger as logger
|
||||
from openhands.server.types import AppMode, ServerConfigInterface
|
||||
from openhands.utils.import_utils import get_impl
|
||||
|
||||
|
||||
class ServerConfig(ServerConfigInterface):
|
||||
config_cls = os.environ.get('OPENHANDS_CONFIG_CLS', None)
|
||||
app_mode = AppMode.OSS
|
||||
posthog_client_key = 'phc_3ESMmY9SgqEAGBB6sMGK5ayYHkeUuknH2vP6FmWH9RA'
|
||||
github_client_id = os.environ.get('GITHUB_APP_CLIENT_ID', '')
|
||||
settings_store_class: str = (
|
||||
'openhands.storage.settings.file_settings_store.FileSettingsStore'
|
||||
)
|
||||
conversation_store_class: str = (
|
||||
'openhands.storage.conversation.file_conversation_store.FileConversationStore'
|
||||
)
|
||||
conversation_manager_class: str = 'openhands.server.conversation_manager.standalone_conversation_manager.StandaloneConversationManager'
|
||||
|
||||
def verify_config(self):
|
||||
if self.config_cls:
|
||||
raise ValueError('Unexpected config path provided')
|
||||
|
||||
def get_config(self):
|
||||
config = {
|
||||
'APP_MODE': self.app_mode,
|
||||
'GITHUB_CLIENT_ID': self.github_client_id,
|
||||
'POSTHOG_CLIENT_KEY': self.posthog_client_key,
|
||||
}
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def load_server_config():
|
||||
config_cls = os.environ.get('OPENHANDS_CONFIG_CLS', None)
|
||||
logger.info(f'Using config class {config_cls}')
|
||||
|
||||
server_config_cls = get_impl(ServerConfig, config_cls)
|
||||
server_config = server_config_cls()
|
||||
server_config.verify_config()
|
||||
|
||||
return server_config
|
||||
Reference in New Issue
Block a user