Skip to content

Commit

Permalink
Redirect trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Sep 7, 2024
1 parent 66e3f85 commit 878abd9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
64 changes: 41 additions & 23 deletions hypha/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ def detected_response_type(request: Request):


class ASGIRoutingMiddleware:
def __init__(self, app: ASGIApp, route=None, store=None):
def __init__(self, app: ASGIApp, base_path=None, store=None):
self.app = app
assert route is not None, "Route is required"
assert base_path is not None, "Base path is required"
assert store is not None, "Store is required"
route = base_path.rstrip("/") + "/{workspace}/apps/{service_id}/{path:path}"
# Define a route using Starlette's routing system for pattern matching
self.route = Route(route, endpoint=None)
self.store = store
Expand Down Expand Up @@ -494,25 +495,6 @@ async def get_workspace_apps(
content={"success": False, "detail": str(exp)},
)

@app.get(norm_url("/{workspace}/apps/{service_id}"))
async def get_app_info(
workspace: str,
service_id: str,
request: Request,
path: str = None,
user_info: store.login_optional = Depends(store.login_optional),
):
"""Route for checking details of an app."""
if not path:
path = "/"
return await run_app(
workspace=workspace,
service_id=service_id,
request=request,
path=path,
user_info=user_info,
)

@app.get(norm_url("/{workspace}/apps/ws/{path:path}"))
async def get_ws_app_file(
workspace: str,
Expand Down Expand Up @@ -574,10 +556,46 @@ async def get_ws_app_file(

app.add_middleware(
ASGIRoutingMiddleware,
route=norm_url("/{workspace}/apps/{service_id}/{path:path}"),
base_path=base_path,
store=store,
)

@app.get(norm_url("/{workspace}/apps/{service_id}"))
async def get_app_info(
workspace: str,
service_id: str,
mode: str = None,
user_info = Depends(store.login_optional),
):
"""Route for checking details of an app."""
if user_info.check_permission(workspace, UserPermission.read):
raise JSONResponse(
status_code=403,
content={
"success": False,
"detail": (
f"{user_info['username']} has no"
f" permission to access {workspace}"
),
},
)
service_type = await self.store.get_service_type_id(
workspace, service_id
)
if service_type:
# redirect to the app page
return RedirectResponse(
url=f"{base_path.rstrip('/')}/{workspace}/apps/{service_id}/"
)
else:
return JSONResponse(
status_code=404,
content={
"success": False,
"detail": f"App service not found: {service_id}",
},
)

@app.get(norm_url("/{workspace}/apps/{service_id}/{path:path}"))
async def run_app(
workspace: str,
Expand Down Expand Up @@ -671,7 +689,7 @@ async def run_app(
status_code=404,
content={
"success": False,
"detail": f"Service cannot be run as an app: {service_id}",
"detail": f"Service cannot be run as an app: {service_id}, type: {info.type}",
},
)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async def test_asgi_serverapp(fastapi_server, test_user_token):
service = await api.get_service(f"{config.workspace}/hello-fastapi")
assert "serve" in service

response = requests.get(f"{SERVER_URL}/{workspace}/apps/hello-fastapi/")
response = requests.get(f"{SERVER_URL}/{workspace}/apps/hello-fastapi")
assert response.ok
assert response.json()["message"] == "Hello World"

Expand Down

0 comments on commit 878abd9

Please sign in to comment.