Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
Signed-off-by: Romy <[email protected]>
  • Loading branch information
romayalon committed Feb 13, 2025
1 parent 78f2a8f commit cf74865
Showing 1 changed file with 53 additions and 26 deletions.
79 changes: 53 additions & 26 deletions src/endpoint/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit cf74865

Please sign in to comment.