Skip to content

Commit

Permalink
Removed domain fallback and fixed callback error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bmastahac committed Jan 22, 2021
1 parent c83fd33 commit ba163ad
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 61 deletions.
11 changes: 8 additions & 3 deletions components/anchoring/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ function createHandler(server){
return function addAnchor(request, response, next) {


// get the domain configuration based on the domain extracted from anchorId. if no domain found fallback on default
const domainConfig = require("./utils").getAnchoringDomainConfig(request.params.anchorId);
// get the domain configuration based on the domain extracted from anchorId.
const receivedDomain = require('./utils').getDomainFromKeySSI(request.params.anchorId);
const domainConfig = require("./utils").getAnchoringDomainConfig(receivedDomain);
if (!domainConfig)
{
console.log('Anchoring Domain not found : ', receivedDomain);
return response.send(500);
}
//init will receive all the available context information : the whole strategy, body, anchorId from the query and the protocol
let flow = $$.flow.start(domainConfig.type);
//let flow = $$.flow.start('ETH');
flow.init(domainConfig, request.params.anchorId, request.body, server.rootFolder);

// all the available information was passed on init.
Expand Down
16 changes: 0 additions & 16 deletions components/anchoring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ function Anchoring(server) {

AnchorVersions(server);
AnchorSubscribe(server);

//todo : check for body & param
// de verificat daca ce primim in body si parameters este corect si nu e malformat - Pentru entry points apelate din extern
// ancorare - validat body structure
// bricks - nu e nimic de validat. Ce se primeste in stream se pune in brick
// bricksFabric - e folosit intern. Nu validam
// BricksLedger - e folosit intern. Nu validam
// todo : de revizuit operatiile I/O de read/write file codate cu Sync, exceptie bootup. Recodare folosind callback
// discutat locatie fisiere. Momentan e folosit server.rootFolder. De revizuit codul ca toate sa fie relativ la server.rootFolder
// pentru deployment locatia va fi mounted. Se poate folosi similar cu implementarea de la bricks :
//if (typeof process.env.EDFS_BRICK_STORAGE_FOLDER !== 'undefined') {
// storageFolder = process.env.EDFS_BRICK_STORAGE_FOLDER;
//}
// Intrebare : Ar fi ok sa folosesc aceiasi abordare pentru tot, folosind intrari diferite pentru fiecare entrypoint?
// validare date primite din exterior
// revizuire : https://privatesky.xyz/?API/api-hub/overview
}

module.exports = Anchoring;
15 changes: 3 additions & 12 deletions components/anchoring/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
const getAnchoringDomainConfig = (ssiString) => {
const getAnchoringDomainConfig = (domain) => {
const config = require("../../../config");
const domain = getDomainFromKeySSI(ssiString);
let domainConfig = config.getConfig('endpointsConfig', 'anchoring', 'domainStrategies', domain);

if (!domainConfig) {
domainConfig = config.getConfig('endpointsConfig', 'anchoring', 'domainStrategies', 'default');
}

return domainConfig;
return config.getConfig('endpointsConfig', 'anchoring', 'domainStrategies', domain);
};

const getDomainFromKeySSI = function (ssiString) {
const openDSU = require("opendsu");
const keySSISpace = openDSU.loadApi("keyssi");

const keySSI = keySSISpace.parse(ssiString);
const domain = keySSI.getDLDomain();
return domain;
return keySSI.getDLDomain();
}

module.exports = {getAnchoringDomainConfig, getDomainFromKeySSI}
11 changes: 9 additions & 2 deletions components/anchoring/versions/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
function AnchorVersions(server) {

server.get(`/anchor/:domain/versions/:keyssi`, (request, response, next) => {
const domainConfig = require("../utils").getAnchoringDomainConfig(request.params.keyssi);

// get the domain configuration based on the domain extracted from anchorId.
const receivedDomain = require('../utils').getDomainFromKeySSI(request.params.keyssi);
const domainConfig = require("../utils").getAnchoringDomainConfig(receivedDomain);
if (!domainConfig)
{
console.log('Anchoring Domain not found : ', receivedDomain);
return response.send(500);
}
const flow = $$.flow.start(domainConfig.type);
//const flow = $$.flow.start('ETH');
flow.init(domainConfig,request.params.keyssi, request.body, server.rootFolder);
flow.readVersions(request.params.keyssi,server, (err, fileHashes) => {
if (err) {
Expand Down
38 changes: 25 additions & 13 deletions components/bricking/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
function createHandlerUploadBrick(server)
{
return function uploadBrick(request, response, next) {
const flow = $$.flow.start('BricksManager');
//ensure we support both signatures for put-brick. With and without domain. Fallback to 'default'.
const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(request.params.domain);
const domain = require('./utils/index').getSafeDomain(request.params.domain);
const brickDomain = request.params.domain;
const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(brickDomain);
if (!domainConfig)
{
console.log('Brick Domain not found : ', request.params.domain);
return response.send(500);
}

flow.init(domainConfig, domain, server.rootFolder);
const flow = $$.flow.start('BricksManager');
flow.init(domainConfig, brickDomain, server.rootFolder);

flow.write(request, (err, result) => {
if (err) {
Expand All @@ -25,12 +29,16 @@ function createHandlerDownloadBrick(server)
return function downloadBrick(request, response, next) {
response.setHeader('content-type', 'application/octet-stream');
response.setHeader('Cache-control', 'max-age=31536000'); // set brick cache expiry to 1 year
//ensure we support both signatures for put-brick. With and without domain. Fallback to 'default'.
const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(request.params.domain);
const domain = require('./utils/index').getSafeDomain(request.params.domain);
const brickDomain = request.params.domain;
const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(brickDomain);
if (!domainConfig)
{
console.log('Brick Domain not found : ', request.params.domain);
return response.send(500);
}

const flow = $$.flow.start('BricksManager');
flow.init(domainConfig,domain,server.rootFolder);
flow.init(domainConfig,brickDomain,server.rootFolder);

flow.read(request.params.hashLink, response, (err, result) => {
if (err) {
Expand All @@ -48,12 +56,16 @@ function createHandlerDownloadMultipleBricks(server)
return function downloadMultipleBricks(request, response, next) {
response.setHeader('content-type', 'application/octet-stream');
response.setHeader('Cache-control', 'max-age=31536000'); // set brick cache expiry to 1 year
//ensure we support both signatures for put-brick. With and without domain. Fallback to 'default'.
const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(request.params.domain);
const domain = require('./utils/index').getSafeDomain(request.params.domain);
const brickDomain = request.params.domain;
const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(brickDomain);
if (!domainConfig)
{
console.log('Brick Domain not found : ', request.params.domain);
return response.send(500);
}

const flow = $$.flow.start('BricksManager');
flow.init(domainConfig,domain,server.rootFolder);
flow.init(domainConfig,brickDomain,server.rootFolder);
flow.readMultipleBricks(request.query.hashes, response, (err, result) => {
if (err) {
return response.send(404, 'Brick not found');
Expand Down
17 changes: 2 additions & 15 deletions components/bricking/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,17 @@ const getBricksDomainConfigFromKeySSI = (ssiString) => {
};

const getBricksDomainConfigByDomain = (domain) => {
domain = getSafeDomain(domain);
return __getDomainConfig(domain);
};

function __getDomainConfig(domain) {
const config = require("../../../config");
let domainConfig = config.getConfig('endpointsConfig', 'bricking', 'domains', domain);
if (!domainConfig) {
domainConfig = config.getConfig('endpointsConfig', 'bricking', 'domains', 'default');
}

return domainConfig;
}

const getSafeDomain = (domain) => {
if (typeof domain === 'undefined') {
return "default";
}
return domain;
};

const getDomainFromKeySSI = function (ssiString) {
if (typeof ssiString === 'undefined') {
return "default";
}
const openDSU = require("opendsu");
const keySSISpace = openDSU.loadApi("keyssi");

Expand All @@ -36,4 +23,4 @@ const getDomainFromKeySSI = function (ssiString) {
return domain;
};

module.exports = {getBricksDomainConfigFromKeySSI, getDomainFromKeySSI, getBricksDomainConfigByDomain, getSafeDomain};
module.exports = {getBricksDomainConfigFromKeySSI, getDomainFromKeySSI, getBricksDomainConfigByDomain};

0 comments on commit ba163ad

Please sign in to comment.