π OpenHands: AI-Driven Development
add catch all route, disable caching (#4547)
Robert Brennan8c064fe3df9d
diff --git a/frontend/src/api/open-hands.ts b/frontend/src/api/open-hands.tsindex 38bda53e96..0ef84c0278 100644--- a/frontend/src/api/open-hands.ts+++ b/frontend/src/api/open-hands.ts@@ -201,7 +201,7 @@ class OpenHands {static async getGitHubAccessToken(code: string,): Promise<GitHubAccessTokenResponse> {- const response = await fetch(`${OpenHands.BASE_URL}/github/callback`, {+ const response = await fetch(`${OpenHands.BASE_URL}/api/github/callback`, {method: "POST",body: JSON.stringify({ code }),headers: {@@ -218,7 +218,7 @@ class OpenHands {* @returns Whether the user is authenticated*/static async isAuthenticated(login: string): Promise<boolean> {- const response = await fetch(`${OpenHands.BASE_URL}/authenticate`, {+ const response = await fetch(`${OpenHands.BASE_URL}/api/authenticate`, {method: "POST",body: JSON.stringify({ login }),headers: {diff --git a/openhands/server/listen.py b/openhands/server/listen.pyindex a082031205..defbd0d42b 100644--- a/openhands/server/listen.py+++ b/openhands/server/listen.py@@ -34,6 +34,7 @@ from fastapi.responses import JSONResponse, StreamingResponsefrom fastapi.security import HTTPBearerfrom fastapi.staticfiles import StaticFilesfrom pydantic import BaseModel+from starlette.middleware.base import BaseHTTPMiddlewareimport openhands.agenthub # noqa F401 (we import this to get the agents registered)from openhands.controller.agent import Agent@@ -81,6 +82,25 @@ app.add_middleware(allow_headers=['*'],)++class NoCacheMiddleware(BaseHTTPMiddleware):+ """+ Middleware to disable caching for all routes by adding appropriate headers+ """++ async def dispatch(self, request, call_next):+ response = await call_next(request)+ if not request.url.path.startswith('/assets'):+ response.headers['Cache-Control'] = (+ 'no-cache, no-store, must-revalidate, max-age=0'+ )+ response.headers['Pragma'] = 'no-cache'+ response.headers['Expires'] = '0'+ return response+++app.add_middleware(NoCacheMiddleware)+security_scheme = HTTPBearer()@@ -180,9 +200,14 @@ async def attach_session(request: Request, call_next):Returns:Response: The response from the next middleware or route handler."""- if request.url.path.startswith('/api/options/') or not request.url.path.startswith(- '/api/'- ):+ non_authed_paths = [+ '/api/options/',+ '/api/github/callback',+ '/api/authenticate',+ ]+ if any(+ request.url.path.startswith(path) for path in non_authed_paths+ ) or not request.url.path.startswith('/api/'):response = await call_next(request)return response@@ -762,7 +787,7 @@ class AuthCode(BaseModel):code: str[email protected]('/github/callback')[email protected]('/api/github/callback')def github_callback(auth_code: AuthCode):# Prepare data for the token exchange requestdata = {@@ -802,7 +827,7 @@ class User(BaseModel):login: str # GitHub login handle[email protected]('/authenticate')[email protected]('/api/authenticate')def authenticate(user: User | None = None):waitlist = os.getenv('GITHUB_USER_LIST_FILE')@@ -827,4 +852,13 @@ def authenticate(user: User | None = None):)-app.mount('/', StaticFiles(directory='./frontend/build', html=True), name='dist')+class SPAStaticFiles(StaticFiles):+ async def get_response(self, path: str, scope):+ try:+ return await super().get_response(path, scope)+ except Exception:+ # FIXME: just making this HTTPException doesn't work for some reason+ return await super().get_response('index.html', scope)+++app.mount('/', SPAStaticFiles(directory='./frontend/build', html=True), name='dist')