Skip to content

Commit

Permalink
Cluster /{key}/docs endpoint for shareable Swagger UI. (#590)
Browse files Browse the repository at this point in the history
add `/{key}/docs` for Modules. If there is no `openapi_spec` method or the module is not found, we see an error like so:

![image.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/O6PzvaCA4i9G3krtjvRT/f6b7cc21-6372-4e00-931c-fc8733360905.png)
  • Loading branch information
rohinb2 committed Mar 14, 2024
1 parent 0dac015 commit bedbeda
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions runhouse/servers/http/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import yaml
from fastapi import Body, FastAPI, HTTPException, Request
from fastapi.encoders import jsonable_encoder
from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html
from fastapi.responses import StreamingResponse

from runhouse.constants import (
Expand Down Expand Up @@ -47,6 +48,7 @@
from runhouse.servers.obj_store import (
ClusterServletSetupOption,
ObjStore,
ObjStoreError,
RaySetupOption,
)

Expand Down Expand Up @@ -361,6 +363,45 @@ async def _call(
from_http_server=True,
)

# Open API docs routes
@staticmethod
@app.get("/{key}/openapi.json")
@validate_cluster_access
async def get_openapi_spec(request: Request, key: str):
try:
module_openapi_spec = obj_store.call(
key, method_name="openapi_spec", serialization=None
)
except (AttributeError, ObjStoreError):
# The object put on the server is not an `rh.Module`, so it doesn't have an openapi_spec method
# OR
# The object is not found in the object store at all
module_openapi_spec = None

if not module_openapi_spec:
raise HTTPException(status_code=404, detail=f"Module {key} not found.")

return module_openapi_spec

@staticmethod
@app.get("/{key}/redoc")
@validate_cluster_access
async def get_redoc(request: Request, key: str):
return get_redoc_html(
openapi_url=f"/{key}/openapi.json", title="Developer Documentation"
)

@staticmethod
@app.get("/{key}/docs")
@validate_cluster_access
async def get_swagger_ui_html(request: Request, key: str):
return get_swagger_ui_html(
openapi_url=f"/{key}/openapi.json",
title=f"{key} - Swagger UI",
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
init_oauth=app.swagger_ui_init_oauth,
)

# TODO match "/{key}/{method_name}/{path:more_path}" for asgi / proxy requests
@staticmethod
@app.post("/{key}/{method_name}")
Expand Down

0 comments on commit bedbeda

Please sign in to comment.