From cf748659177fe0d25fa751df7d70ffb3ff34db53 Mon Sep 17 00:00:00 2001 From: Romy <35330373+romayalon@users.noreply.github.com> Date: Thu, 13 Feb 2025 11:22:35 +0200 Subject: [PATCH] CR Signed-off-by: Romy <35330373+romayalon@users.noreply.github.com> --- src/endpoint/endpoint.js | 79 +++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/src/endpoint/endpoint.js b/src/endpoint/endpoint.js index bb5a83eaa6..b4d612c626 100755 --- a/src/endpoint/endpoint.js +++ b/src/endpoint/endpoint.js @@ -295,10 +295,13 @@ function create_endpoint_handler(server_type, init_request_sdk, { virtual_hosts, return fork_count_handler(req, res); } else if (req.url.startsWith('/endpoint_fork_id')) { return endpoint_fork_id_handler(req, res); - } else if (req.url.startsWith('/_')) { + } else if (req.url.startsWith('/_/')) { // internals non S3 requests - if (req.url === '/_version') { + const api = req.url.slice('/_/'.length); + if (api === 'version') { return version_handler(req, res); + } else { + return internal_api_error(req, res, `Unknown API call ${api}`); } } else { return s3_rest.handler(req, res); @@ -331,43 +334,67 @@ function create_endpoint_handler(server_type, init_request_sdk, { virtual_hosts, } } +/////////////////////////// +// INTERNAL API HANDLERS // +/////////////////////////// + +/** + * version_handler returns the version of noobaa package + * @param {EndpointRequest} req + * @param {import('http').ServerResponse} res + */ +function version_handler(req, res) { + const noobaa_package_version = pkg.version; + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.setHeader('Content-Length', Buffer.byteLength(noobaa_package_version)); + res.end(noobaa_package_version); +} + +/** + * internal_api_error returns an internal api error response + * @param {EndpointRequest} req + * @param {import('http').ServerResponse} res + * @param {string} error_message + */ +function internal_api_error(req, res, error_message) { + const buffer = Buffer.from(JSON.stringify({ error: 'Internal Server Error', message: error_message })); + res.statusCode = 500; + res.setHeader('Content-Type', 'application/json'); + res.setHeader('Content-Length', buffer.length); + res.end(buffer); +} + +/** + * endpoint_fork_id_handler returns the worker id of the current fork + * @param {EndpointRequest} req + * @param {import('http').ServerResponse} res + */ function endpoint_fork_id_handler(req, res) { let reply = {}; if (cluster.isWorker) { - reply = { - worker_id: cluster.worker.id, - }; + reply = { worker_id: cluster.worker.id }; } P.delay(500); res.statusCode = 200; + const buffer = Buffer.from(JSON.stringify(reply)); res.setHeader('Content-Type', 'application/json'); - res.setHeader('Content-Length', Buffer.byteLength(JSON.stringify(reply))); - res.end(JSON.stringify(reply)); -} - -function fork_count_handler(req, res) { - const reply = { - fork_count: fork_count, - }; - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.setHeader('Content-Length', Buffer.byteLength(JSON.stringify(reply))); - res.end(JSON.stringify(reply)); + res.setHeader('Content-Length', buffer.length); + res.end(buffer); } /** - * version_handler return the version of noobaa package - * @param {*} req - * @param {*} res + * fork_count_handler returns the total number of forks + * @param {EndpointRequest} req + * @param {import('http').ServerResponse} res */ -function version_handler(req, res) { - const reply = { - version: pkg.version, - }; +function fork_count_handler(req, res) { + const reply = { fork_count: fork_count }; res.statusCode = 200; + const buffer = Buffer.from(JSON.stringify(reply)); res.setHeader('Content-Type', 'application/json'); - res.setHeader('Content-Length', Buffer.byteLength(JSON.stringify(reply))); - res.end(JSON.stringify(reply)); + res.setHeader('Content-Length', buffer.length); + res.end(buffer); } /**