OpenHands icon indicating copy to clipboard operation
OpenHands copied to clipboard

Refactor listen.py into sub-routes

Open rbren opened this issue 1 year ago • 1 comments

End-user friendly description of the problem this fixes or functionality that this introduces

  • [ ] Include this change in the Release Notes. If checked, you must provide an end-user friendly description for your change below no changelog

Give a summary of what the PR does, explaining any non-trivial design decisions

This is a WIP, but appears to be functioning well.

listen.py has grown pretty unwieldy, with almost 1k LoC. This breaks up different routes into their own APIRouters, which makes it easier for us to e.g. attach different middleware to each one

Hypothetically we could make each subset of routes its own FastAPI object (instead of just an APIRouter), which would make it MUCH easier to do middleware. But those need to be mounted on unique prefixes, and we'd have to wreck the API to do that. Maybe worth it? TBD. See the AttachSessionMiddleware in restricted.py for why this kind of sucks.

New listen.py is very small, just

app = FastAPI()
app.add_middleware(
    LocalhostCORSMiddleware,
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)


app.add_middleware(NoCacheMiddleware)

app.include_router(auth_api_router)
app.include_router(public_api_router)
app.include_router(restricted_api_router)
app.include_router(websocket_router)

app.middleware('http')(
    AttachSessionMiddleware(app, target_router=restricted_api_router)
)

app.mount('/', SPAStaticFiles(directory='./frontend/build', html=True), name='dist')

Link of any specific issues this addresses


To run this PR locally, use the following command:

docker run -it --rm   -p 3000:3000   -v /var/run/docker.sock:/var/run/docker.sock   --add-host host.docker.internal:host-gateway   -e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:9442451-nikolaik   --name openhands-app-9442451   docker.all-hands.dev/all-hands-ai/openhands:9442451

rbren avatar Nov 13 '24 19:11 rbren

Gentle ping for @rbren. Is this something you will get back to?

mamoodi avatar Nov 25 '24 21:11 mamoodi

On it today! Will probably recreate

rbren avatar Nov 26 '24 12:11 rbren

Closing in favor of https://github.com/All-Hands-AI/OpenHands/pull/5281

rbren avatar Nov 26 '24 13:11 rbren