Skip to content

Commit 3f5fa32

Browse files
committed
nodes may have api_dev_url besides api_url
1 parent 2807034 commit 3f5fa32

6 files changed

Lines changed: 47 additions & 23 deletions

File tree

‎src/database/index.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ const {
1414
// Get the ping function
1515
const ping = require('../utils/ping');
1616
// Standard HTTP response status codes
17-
const { NOT_FOUND, BAD_REQUEST } = require('../utils/status-codes');
17+
const { NOT_FOUND, BAD_REQUEST, INTERNAL_SERVER_ERROR } = require('../utils/status-codes');
1818
// The project class is used to handle database data from a specific project
1919
const Project = require('./project');
20+
// The node class is used to handle database data from a specific node
21+
const Node = require('./node');
2022

2123
// Database.ObjectId returns an object with the mongo object id
2224
// This id is associated to the provided idOrAccession when it is valid
@@ -162,6 +164,20 @@ class Database4Api extends Database {
162164
return new Project(projectData, this);
163165
}
164166

167+
// Get a node by its alias
168+
// If there is any problem send informative errors
169+
getNode = async nodeAlias => {
170+
// Get node data from the database
171+
const nodeData = await this.nodes.findOne({ alias: nodeAlias });
172+
// If we did not found the node then stop here
173+
if (!nodeData) return {
174+
headerError: INTERNAL_SERVER_ERROR,
175+
error: `Node "${nodeAlias}" not found`
176+
};
177+
// Return the node handler
178+
return new Node(nodeData, this);
179+
}
180+
165181
// Get all ids available in a given reference
166182
getReferenceAvailableIds = async (referenceName, query = {}) => {
167183
// Get the requested reference configuration

‎src/database/node/index.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Set the node class
2+
// A node is one of the databases federated under the global API
3+
class Node {
4+
constructor (data, database) {
5+
// Store the current node data
6+
this.data = data;
7+
this.alias = this.data.alias;
8+
// Store the database handler
9+
this.database = database;
10+
// Outside production use the development API url, if any
11+
// Note that all nodes have the 'api_url' but only some of them have the 'api_dev_url'
12+
const isProduction = this.database.config.production;
13+
this.apiUrl = (!isProduction && this.data.api_dev_url) || this.data.api_url;
14+
};
15+
}
16+
17+
module.exports = Node;

‎src/routes/knowledge/index.js‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,16 @@ const redirectHandler = handler({
141141
// Find the database thes project belongs to
142142
const nodeAlias = projectData.node;
143143
// Get the corresponding node
144-
const node = await database.nodes.findOne({ alias: nodeAlias });
145-
if (!node) return {
146-
headerError: INTERNAL_SERVER_ERROR,
147-
error: `Node "${nodeAlias}" not found`
148-
};
144+
const node = await database.getNode(nodeAlias);
145+
if (node.error) return node;
149146
// Get url path removing the first slash
150147
const urlPath = request.originalUrl.substring(1);
151148
// Replace the global id by the local id
152149
const splittedPath = urlPath.split('/');
153150
splittedPath[4] = localAccession;
154151
const replacedPath = splittedPath.join('/');
155152
// Build the new forwarded URL using the corresponding node API url
156-
const forwardedRef = node.api_url + replacedPath;
153+
const forwardedRef = node.apiUrl + replacedPath;
157154
// The response code must change depending on the request method
158155
let code;
159156
if (request.method === 'GET') code = 302;

‎src/routes/optimade/utils.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ function applyResponseFields(data, responseFieldsParam) {
109109
// localPath: the OPTIMADE path segment after /optimade/v1/ using the LOCAL accession.
110110
async function resolveLocalOptimadeUrl(database, project, localPath, request) {
111111
if (!project.node) return null;
112-
const nodeDoc = await database.nodes.findOne({ alias: project.node });
113-
if (!nodeDoc?.api_url) return null;
114-
const nodeUrl = nodeDoc.api_url;
112+
const node = await database.getNode(project.node);
113+
if (node.error || !node.apiUrl) return null;
114+
const nodeUrl = node.apiUrl;
115115
const origin = new URL(nodeUrl).origin;
116116
const currentOrigin = `${request.protocol}://${request.get('host')}`;
117117
// Same host (e.g. development) — no redirect needed, data is available locally

‎src/routes/projects/index.js‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const projectFormatter = require('../../utils/project-formatter');
88
// Get auxiliar functions
99
const { parseJSON, getConfig, parseType, getSearchQuery } = require('../../utils/auxiliar-functions');
1010
// Standard HTTP response status codes
11-
const { BAD_REQUEST, INTERNAL_SERVER_ERROR } = require('../../utils/status-codes');
11+
const { BAD_REQUEST } = require('../../utils/status-codes');
1212

1313
const projectRouter = Router();
1414

@@ -280,19 +280,16 @@ const redirectHandler = handler({
280280
// Find the database thes project belongs to
281281
const nodeAlias = projectData.node;
282282
// Get the corresponding node
283-
const node = await database.nodes.findOne({ alias: nodeAlias });
284-
if (!node) return {
285-
headerError: INTERNAL_SERVER_ERROR,
286-
error: `Node "${nodeAlias}" not found`
287-
};
283+
const node = await database.getNode(nodeAlias);
284+
if (node.error) return node;
288285
// Get url path removing the first slash
289286
const urlPath = request.originalUrl.substring(1);
290287
// Replace the global id by the local id
291288
const splittedPath = urlPath.split('/');
292289
splittedPath[3] = localAccession;
293290
const replacedPath = splittedPath.join('/');
294291
// Build the new forwarded URL using the corresponding node API url
295-
const forwardedRef = node.api_url + replacedPath;
292+
const forwardedRef = node.apiUrl + replacedPath;
296293
// The response code must change depending on the request method
297294
let code;
298295
if (request.method === 'GET') code = 302;

‎src/routes/references/index.js‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,12 @@ const redirectHandler = handler({
276276
error: `The "${referenceName}" reference "${referenceId}" is missing the node field.`
277277
};
278278
// Get the corresponding node
279-
const node = await database.nodes.findOne({ alias: nodeAlias });
280-
if (!node) return {
281-
headerError: INTERNAL_SERVER_ERROR,
282-
error: `Node "${nodeAlias}" not found`
283-
};
279+
const node = await database.getNode(nodeAlias);
280+
if (node.error) return node;
284281
// Get url path removing the first slash
285282
const urlPath = request.originalUrl.substring(1);
286283
// Build the new forwarded URL using the corresponding node API url
287-
const forwardedRef = node.api_url + urlPath;
284+
const forwardedRef = node.apiUrl + urlPath;
288285
// The response code must change depending on the request method
289286
let code;
290287
if (request.method === 'GET') code = 302;

0 commit comments

Comments
 (0)