Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

fix: Using settings.base_url to setup base url path #14

Merged
merged 8 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ These are the section headers that we use:

## [Unreleased]()

### Fixed

- Fixed problems using `ARGILLA_BASE_URL` environment variable. ([#14](https://github.com/argilla-io/argilla-server/pull/14))

## [1.23.0](https://github.com/argilla-io/argilla/compare/v1.22.0...v1.23.0)

### Added
Expand Down
5 changes: 4 additions & 1 deletion src/argilla_server/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ async def redirect_api():
]:
app_configure(app)

return app
_app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
_app.mount(settings.base_url, app)

return _app


def configure_middleware(app: FastAPI):
Expand Down
5 changes: 1 addition & 4 deletions src/argilla_server/static_rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class RewriteStaticFiles(StaticFiles):

async def get_response(self, path: str, scope: Scope) -> Response:
try:
# Normalize path to avoid redirections
if not scope["path"].endswith("/"):
scope["path"] += "/"
response = await super().get_response(path, scope)
return await self._handle_response(response, scope)
except HTTPException as ex:
Expand All @@ -39,7 +36,7 @@ async def _handle_response(
scope,
):
if self.html and (response_or_error.status_code == 404):
return await super().get_response(path="", scope=scope)
return await super().get_response(path="/", scope=scope)
if isinstance(response_or_error, HTTPException):
raise response_or_error
return response_or_error
51 changes: 51 additions & 0 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import cast

import pytest
from argilla_server._app import create_server_app
from argilla_server.settings import Settings, settings
from starlette.routing import Mount
from starlette.testclient import TestClient


@pytest.fixture
def test_settings():
yield settings

settings.base_url = "/"


class TestApp:
def test_create_app_with_base_url(self, test_settings: Settings):
base_url = "/base/url"
settings.base_url = base_url

app = create_server_app()
client = TestClient(app)

response = client.get("/api/docs")
assert response.status_code == 404

response = client.get(f"{base_url}/api/docs")
assert response.status_code == 200

response = client.get("/api/_info")
assert response.status_code == 404

response = client.get(f"{base_url}/api/_info")
assert response.status_code == 200

assert len(app.routes) == 1
assert cast(Mount, app.routes[0]).path == base_url
Loading