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 base URL to configuration file #135

Merged
merged 6 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions optimade/server/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ STRUCTURES_COLLECTION = structures
[SERVER]
PAGE_LIMIT = 500
DEFAULT_DB = test_server
BASE_URL = http://localhost:5000
CasperWA marked this conversation as resolved.
Show resolved Hide resolved

[IMPLEMENTATION]
name = Example implementation
Expand Down
10 changes: 7 additions & 3 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def _DEFAULTS(field: str) -> Any:
"structures_collection": "structures",
"page_limit": 20,
"default_db": "test_server",
"base_url": None,
"implementation": {
"name": "Example implementation",
"version": __version__,
Expand All @@ -115,8 +116,8 @@ def _DEFAULTS(field: str) -> Any:
"prefix": "_exmpl_",
"name": "Example provider",
"description": "Provider used for examples, not to be assigned to a real database",
"homepage": "http://example.com",
"index_base_url": "http://example.com/optimade/index",
"homepage": "https://example.com",
"index_base_url": "https://example.com/index/optimade",
CasperWA marked this conversation as resolved.
Show resolved Hide resolved
},
}
if field not in res:
Expand All @@ -141,6 +142,9 @@ def load_from_ini(self):
self.default_db = config.get(
"SERVER", "DEFAULT_DB", fallback=self._DEFAULTS("default_db")
)
self.base_url = config.get(
"SERVER", "BASE_URL", fallback=self._DEFAULTS("base_url")
)

# This is done in this way, since each field is OPTIONAL
self.implementation = {}
Expand Down Expand Up @@ -196,8 +200,8 @@ def load_from_json(self):
)

self.page_limit = int(config.get("page_limit", self._DEFAULTS("page_limit")))
self.version = config.get("api_version", self._DEFAULTS("api_version"))
self.default_db = config.get("default_db", self._DEFAULTS("default_db"))
self.base_url = config.get("base_url", self._DEFAULTS("base_url"))

# This is done in this way, since each field is OPTIONAL
self.implementation = config.get("implementation", {})
Expand Down
13 changes: 12 additions & 1 deletion optimade/server/routers/info.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import urllib

from typing import Union

from fastapi import APIRouter
Expand All @@ -14,6 +16,8 @@
StructureResource,
)

from optimade.server.config import CONFIG

from .utils import meta_values, retrieve_queryable_properties


Expand All @@ -34,6 +38,13 @@
def get_info(request: Request):
from optimade.models import BaseInfoResource, BaseInfoAttributes

parse_result = urllib.parse.urlparse(str(request.url))
base_url = (
CONFIG.base_url
if CONFIG.base_url
else f"{parse_result.scheme}://{parse_result.netloc}"
Copy link
Member

Choose a reason for hiding this comment

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

Oh, so you get the base URL from the request?

I wonder - would that solve the problem of having to specify the base URL?

Copy link
Member Author

Choose a reason for hiding this comment

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

No.
The else value is what used to be up to now, and is giving false information if run in a docker (it will provide you with something like http://localhost:3585 or whatever the port is).

)

return InfoResponse(
meta=meta_values(str(request.url), 1, 1, more_data_available=False),
data=BaseInfoResource(
Expand All @@ -43,7 +54,7 @@ def get_info(request: Request):
api_version=f"v{__api_version__}",
available_api_versions=[
{
"url": f"http://localhost:5000/optimade/v{__api_version__}/",
"url": f"{base_url}/optimade/v{__api_version__}",
"version": __api_version__,
}
],
Expand Down
11 changes: 8 additions & 3 deletions optimade/server/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,14 @@ def get_entries(
query = urllib.parse.parse_qs(parse_result.query)
query["page_offset"] = int(query.get("page_offset", [0])[0]) + len(results)
urlencoded = urllib.parse.urlencode(query, doseq=True)
links = ToplevelLinks(
next=f"{parse_result.scheme}://{parse_result.netloc}{parse_result.path}?{urlencoded}"
)
if CONFIG.base_url:
if parse_result.netloc.split("/")[1:]:
base_url = f"{CONFIG.base_url}/{parse_result.netloc.split('/')[1:]}"
CasperWA marked this conversation as resolved.
Show resolved Hide resolved
else:
base_url = CONFIG.base_url
else:
base_url = f"{parse_result.scheme}://{parse_result.netloc}"
links = ToplevelLinks(next=f"{base_url}{parse_result.path}?{urlencoded}")
else:
links = ToplevelLinks(next=None)

Expand Down