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

Reflect CONFIG.root_path in get_base_url #663

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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: 11 additions & 0 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ def set_implementation_version(cls, v):
res.update(v)
return res

@validator("root_path", pre=False)
def remove_end_slashes(cls, value: str) -> str:
"""Remove ending slashes from root_path"""
if isinstance(value, str):
while value.endswith("/"):
value = value[:-1]
while value.startswith("/"):
value = value[1:]
Comment on lines +280 to +283
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while value.endswith("/"):
value = value[:-1]
while value.startswith("/"):
value = value[1:]
value = value.strip("/")

I think it would be better to use strip here as it is shorter.

value = f"/{value}"
return value

@root_validator(pre=True)
def use_real_mongo_override(cls, values):
"""Overrides the `database_backend` setting with MongoDB and
Expand Down
8 changes: 5 additions & 3 deletions optimade/server/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ def get_base_url(
if isinstance(parsed_url_request, str)
else parsed_url_request
)
root_path = CONFIG.root_path.rstrip("/") if CONFIG.root_path else ""
return (
CONFIG.base_url.rstrip("/")
if CONFIG.base_url
else f"{parsed_url_request.scheme}://{parsed_url_request.netloc}"
else f"{parsed_url_request.scheme}://{parsed_url_request.netloc}{root_path}"
)


Expand All @@ -213,9 +214,10 @@ def get_entries(
query = urllib.parse.parse_qs(request.url.query)
query["page_offset"] = int(query.get("page_offset", [0])[0]) + len(results)
urlencoded = urllib.parse.urlencode(query, doseq=True)
base_url = get_base_url(request.url)
root_path = CONFIG.root_path.rstrip("/") if CONFIG.root_path else ""
url = f"{get_base_url(request.url)}{request.url.path.replace(root_path, '')}"

links = ToplevelLinks(next=f"{base_url}{request.url.path}?{urlencoded}")
links = ToplevelLinks(next=f"{url}?{urlencoded}")
else:
links = ToplevelLinks(next=None)

Expand Down
1 change: 1 addition & 0 deletions tests/test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"debug": false,
"default_db": "test_server",
"base_url": "http://example.org",
"root_path": "custom_root_path/",
"implementation": {
"name": "Example implementation",
"source_url": "https://github.com/Materials-Consortia/optimade-python-tools",
Expand Down