Skip to content

Commit

Permalink
Add mount_path for mounting app at sub-path (#334)
Browse files Browse the repository at this point in the history
* Add mount_path for mounting app at sub-path

* Add test
  • Loading branch information
davidbrochart authored Jul 27, 2023
1 parent e42e03c commit 7b19d8e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
11 changes: 9 additions & 2 deletions jupyverse_api/jupyverse_api/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
from collections import defaultdict
from typing import Dict, List
Expand All @@ -16,8 +18,13 @@ class App:
_app: FastAPI
_router_paths: Dict[str, List[str]]

def __init__(self, app: FastAPI):
self._app = app
def __init__(self, app: FastAPI, mount_path: str | None = None):
if mount_path is None:
self._app = app
else:
subapi = FastAPI()
app.mount(mount_path, subapi)
self._app = subapi
app.add_exception_handler(RedirectException, _redirect_exception_handler)
self._router_paths = defaultdict(list)

Expand Down
10 changes: 9 additions & 1 deletion jupyverse_api/jupyverse_api/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@


class AppComponent(Component):
def __init__(
self,
*,
mount_path: str | None = None,
) -> None:
super().__init__()
self.mount_path = mount_path

async def start(
self,
ctx: Context,
) -> None:
app = await ctx.request_resource(FastAPI)

_app = App(app)
_app = App(app, mount_path=self.mount_path)
ctx.add_resource(_app)


Expand Down
46 changes: 46 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest
from asphalt.core import Context
from fastapi import APIRouter
from httpx import AsyncClient
from jupyverse_api import Router
from jupyverse_api.app import App
from jupyverse_api.main import JupyverseComponent

from utils import configure


@pytest.mark.asyncio
@pytest.mark.parametrize(
"mount_path",
(
None,
"/foo",
),
)
async def test_mount_path(mount_path, unused_tcp_port):
components = configure({"app": {"type": "app"}}, {"app": {"mount_path": mount_path}})

async with Context() as ctx, AsyncClient() as http:
await JupyverseComponent(
components=components,
port=unused_tcp_port,
).start(ctx)

app = await ctx.request_resource(App)
router = APIRouter()

@router.get("/")
async def get():
pass

Router(app).include_router(router)

response = await http.get(f"http://127.0.0.1:{unused_tcp_port}")
expected = 200 if mount_path is None else 404
assert response.status_code == expected

response = await http.get(f"http://127.0.0.1:{unused_tcp_port}/bar")
assert response.status_code == 404

response = await http.get(f"http://127.0.0.1:{unused_tcp_port}/foo")
expected = 404 if mount_path is None else 200

0 comments on commit 7b19d8e

Please sign in to comment.