Skip to content

Commit 87f0b58

Browse files
committed
yet another try to fix optimade
1 parent a9ecf42 commit 87f0b58

2 files changed

Lines changed: 7 additions & 11 deletions

File tree

‎src/routes/optimade/base-html.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// Returns the OPTIMADE HTML landing page for base URLs (/optimade/ and /optimade/v1/).
22
// Per the OPTIMADE spec, the base URL should return a human-readable page when accessed
33
// via a browser. JSON data for programmatic access is available at /optimade/v1/info.
4-
const { OPTIMADE_VERSION, PROVIDER, IMPLEMENTATION } = require('./utils');
4+
const { OPTIMADE_VERSION, PROVIDER, IMPLEMENTATION, getBaseUrl } = require('./utils');
55

66
// Build the URL carefully so it works both locally and behind a reverse proxy
77
function getBaseHtml(request) {
8-
const proto = request.get('x-forwarded-proto') || request.protocol;
9-
const host = request.get('host');
10-
const match = request.originalUrl.match(/^(.*\/optimade)/);
11-
const base = `${proto}://${host}${match ? match[1] : '/optimade'}/v1`;
8+
const base = getBaseUrl(request);
129

1310
const endpointLinks = ['links', 'references', 'trajectories', 'structures', 'info']
1411
.map(ep => `<li><a href="${base}/${ep}">${base}/${ep}</a></li>`)

‎src/routes/optimade/utils.js‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ function buildResponse({ data, meta, links }) {
4343
}
4444

4545
function getBaseUrl(request) {
46-
const proto = request.get('x-forwarded-proto') || request.protocol;
4746
const host = request.get('host');
48-
// Extract everything up to /optimade (preserving any reverse-proxy prefix like /api),
49-
// then always append /v1 so the base is correct regardless of where the request landed.
50-
const match = request.originalUrl.match(/^(.*\/optimade)/);
51-
const prefix = match ? match[1] : '/optimade';
52-
return `${proto}://${host}${prefix}/v1`;
47+
if (host?.match(/^localhost(:\d+)?$/)) {
48+
return `http://${host}/optimade/v1`;
49+
}
50+
const proto = request.get('x-forwarded-proto') || 'https';
51+
return `${proto}://${host}/api/optimade/v1`;
5352
}
5453

5554
// Returns the portion of the URL relative to /optimade/v1

0 commit comments

Comments
 (0)