Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mount_path for mounting app at sub-path #334

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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