|
6 | 6 | require('../util/dotenv').load();
|
7 | 7 | require('../util/panic');
|
8 | 8 | require('../util/fips');
|
| 9 | +const pkg = require('../../package.json'); |
9 | 10 |
|
10 | 11 | const dbg = require('../util/debug_module')(__filename);
|
11 | 12 | if (!dbg.get_process_name()) dbg.set_process_name('Endpoint');
|
@@ -294,6 +295,14 @@ function create_endpoint_handler(server_type, init_request_sdk, { virtual_hosts,
|
294 | 295 | return fork_count_handler(req, res);
|
295 | 296 | } else if (req.url.startsWith('/endpoint_fork_id')) {
|
296 | 297 | return endpoint_fork_id_handler(req, res);
|
| 298 | + } else if (req.url.startsWith('/_/')) { |
| 299 | + // internals non S3 requests |
| 300 | + const api = req.url.slice('/_/'.length); |
| 301 | + if (api === 'version') { |
| 302 | + return version_handler(req, res); |
| 303 | + } else { |
| 304 | + return internal_api_error(req, res, `Unknown API call ${api}`); |
| 305 | + } |
297 | 306 | } else {
|
298 | 307 | return s3_rest.handler(req, res);
|
299 | 308 | }
|
@@ -325,28 +334,67 @@ function create_endpoint_handler(server_type, init_request_sdk, { virtual_hosts,
|
325 | 334 | }
|
326 | 335 | }
|
327 | 336 |
|
| 337 | +/////////////////////////// |
| 338 | +// INTERNAL API HANDLERS // |
| 339 | +/////////////////////////// |
| 340 | + |
| 341 | +/** |
| 342 | + * version_handler returns the version of noobaa package |
| 343 | + * @param {EndpointRequest} req |
| 344 | + * @param {import('http').ServerResponse} res |
| 345 | + */ |
| 346 | +function version_handler(req, res) { |
| 347 | + const noobaa_package_version = pkg.version; |
| 348 | + res.statusCode = 200; |
| 349 | + res.setHeader('Content-Type', 'text/plain'); |
| 350 | + res.setHeader('Content-Length', Buffer.byteLength(noobaa_package_version)); |
| 351 | + res.end(noobaa_package_version); |
| 352 | +} |
| 353 | + |
| 354 | +/** |
| 355 | + * internal_api_error returns an internal api error response |
| 356 | + * @param {EndpointRequest} req |
| 357 | + * @param {import('http').ServerResponse} res |
| 358 | + * @param {string} error_message |
| 359 | + */ |
| 360 | +function internal_api_error(req, res, error_message) { |
| 361 | + const buffer = Buffer.from(JSON.stringify({ error: 'Internal Server Error', message: error_message })); |
| 362 | + res.statusCode = 500; |
| 363 | + res.setHeader('Content-Type', 'application/json'); |
| 364 | + res.setHeader('Content-Length', buffer.length); |
| 365 | + res.end(buffer); |
| 366 | +} |
| 367 | + |
| 368 | +/** |
| 369 | + * endpoint_fork_id_handler returns the worker id of the current fork |
| 370 | + * @param {EndpointRequest} req |
| 371 | + * @param {import('http').ServerResponse} res |
| 372 | + */ |
328 | 373 | function endpoint_fork_id_handler(req, res) {
|
329 | 374 | let reply = {};
|
330 | 375 | if (cluster.isWorker) {
|
331 |
| - reply = { |
332 |
| - worker_id: cluster.worker.id, |
333 |
| - }; |
| 376 | + reply = { worker_id: cluster.worker.id }; |
334 | 377 | }
|
335 | 378 | P.delay(500);
|
336 | 379 | res.statusCode = 200;
|
| 380 | + const buffer = Buffer.from(JSON.stringify(reply)); |
337 | 381 | res.setHeader('Content-Type', 'application/json');
|
338 |
| - res.setHeader('Content-Length', Buffer.byteLength(JSON.stringify(reply))); |
339 |
| - res.end(JSON.stringify(reply)); |
| 382 | + res.setHeader('Content-Length', buffer.length); |
| 383 | + res.end(buffer); |
340 | 384 | }
|
341 | 385 |
|
| 386 | +/** |
| 387 | + * fork_count_handler returns the total number of forks |
| 388 | + * @param {EndpointRequest} req |
| 389 | + * @param {import('http').ServerResponse} res |
| 390 | + */ |
342 | 391 | function fork_count_handler(req, res) {
|
343 |
| - const reply = { |
344 |
| - fork_count: fork_count, |
345 |
| - }; |
| 392 | + const reply = { fork_count: fork_count }; |
346 | 393 | res.statusCode = 200;
|
| 394 | + const buffer = Buffer.from(JSON.stringify(reply)); |
347 | 395 | res.setHeader('Content-Type', 'application/json');
|
348 |
| - res.setHeader('Content-Length', Buffer.byteLength(JSON.stringify(reply))); |
349 |
| - res.end(JSON.stringify(reply)); |
| 396 | + res.setHeader('Content-Length', buffer.length); |
| 397 | + res.end(buffer); |
350 | 398 | }
|
351 | 399 |
|
352 | 400 | /**
|
|
0 commit comments