Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
pantsel committed Jun 9, 2018
1 parent fb3fd78 commit 41417a0
Showing 1 changed file with 124 additions and 99 deletions.
223 changes: 124 additions & 99 deletions api/services/KongProxyHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,14 @@ var self = module.exports = {

beforeEntityUpdate: function(entityName, entityId, connectionId, data, next) {
sails.log.debug("KongProxyHooks:beforeEntityUpdate called()");
if(!entityName || !data.extras || !sails.models["kong" + entityName]) return next(null, data);

sails.models["kong" + entityName].updateOrCreate({
kong_node_id: connectionId,
service_id: entityId
},_.merge(data.extras, {
kong_node_id: connectionId,
service_id: entityId
}), function (err, extras) {
if(err) {
sails.log.error(err);
return next(err);
}
// Delete the extras attr
delete data.extras;
return next(null, data);
});
if(!entityName || !data.extras || !sails.models["kong" + entityName]
|| !self.hooks[entityName] || !self.hooks[entityName].beforeUpdate) return next(null, data);
return self.hooks[entityName].beforeUpdate(entityId, connectionId, data, next);
},

/**
* After Hooks
* ToDo: apply more Hooks when needed
* ToDo: apply more Hooks if needed
*/

afterEntityRetrieve: function(entityName, req, data, next) {
Expand All @@ -44,99 +30,138 @@ var self = module.exports = {
},

afterEntityFetch: function(entityName, req, data, next) {
sails.log.debug("KongProxyHooks:beforeEntityFetch called()", entityName);
if(!entityName || !sails.models["kong" + entityName]) return next(null, data);

var connectionId = req.connection.id;
var entityId = req.path.split("/").filter(function (e) {
return e;
})[1];

sails.models["kong" + entityName].findOne({
kong_node_id: connectionId,
service_id: entityId
}, function (err, extras) {
if(err) {
sails.log.error(err);
return next(err);
}

if(!extras) return next(null, data);

// Add the extras attr
data.extras = extras;
return next(null, data);
});
sails.log.debug("KongProxyHooks:afterEntityFetch called()", entityName);
if(!entityName || !sails.models["kong" + entityName]
|| !self.hooks[entityName] || !self.hooks[entityName].afterFetch) return next(null, data);
return self.hooks[entityName].afterFetch( req, data, next);
},

afterEntityList: function(entityName, req, resBody, next) {
sails.log.debug("KongProxyHooks:afterEntityList called()", entityName);
if(!entityName || !sails.models["kong" + entityName]) return next(null, resBody);

var connectionId = req.connection.id;

sails.models["kong" + entityName].find({
kong_node_id: connectionId
},function (err, extras) {
if (err) {
sails.log.error(err);
return next(null, resBody); // let it pass without the extra appends. No need to block the response
}

// Assign the extras to the services
resBody.data.forEach(function (service) {
service.extras = _.find(extras, function (extra) {
return extra.service_id === service.id
}) || {}
})
return next(null, resBody);
});
if(!entityName || !sails.models["kong" + entityName]
|| !self.hooks[entityName] || !self.hooks[entityName].afterList) return next(null, resBody);
return self.hooks[entityName].afterList( req, resBody, next);
},

afterEntityCreate: function(entityName, req, data, konga_extras, next) {
sails.log.debug("KongProxyHooks:afterEntityCreate called()", entityName);
if(!entityName || !sails.models["kong" + entityName]) return next(null, data);

var connectionId = req.connection.id;
var entityId = data.id;

sails.models["kong" + entityName].create(_.merge({
kong_node_id: connectionId,
service_id: entityId
}, konga_extras), function (err, extras) {
if(err) {
sails.log.error(err);
return next(err);
}

// Add the extras attr
data.extras = extras;
return next(null, data);
});
if(!entityName || !sails.models["kong" + entityName]
|| !self.hooks[entityName] || !self.hooks[entityName].afterCreate) return next(null, data);
return self.hooks[entityName].afterCreate(req, data, konga_extras, next)
},

afterEntityDelete: function(entityName, req, next) {

sails.log.debug("KongProxyHooks:afterEntityDelete called()", entityName);
if(!entityName || !sails.models["kong" + entityName]) return next();

var connectionId = req.connection.id;
// The path must be of type /kong/<entityName>/<entityId>
var entityId = req.path.replace("/kong","").split("/").filter(function (e) {
return e;
})[1];

sails.models["kong" + entityName].destroy({
kong_node_id: connectionId,
service_id: entityId
},function (err) {
if(err) {
sails.log.error("Failed to remove kong service extras",err);
return next(err);
}

return next();
})
if(!entityName || !sails.models["kong" + entityName]
|| !self.hooks[entityName] || !self.hooks[entityName].afterDelete) return next();
return self.hooks[entityName].afterDelete(req, next)

},

hooks: {
services : {
beforeUpdate: function(entityId, connectionId, data, next) {

sails.models.kongservices.updateOrCreate({
kong_node_id: connectionId,
service_id: entityId
},_.merge(data.extras, {
kong_node_id: connectionId,
service_id: entityId
}), function (err, extras) {
if(err) {
sails.log.error(err);
return next(err);
}
// Delete the extras attr
delete data.extras;
return next(null, data);
});
},
afterList: function(req, resBody, next) {

var connectionId = req.connection.id;

sails.models.kongservices.find({
kong_node_id: connectionId
},function (err, extras) {
if (err) {
sails.log.error(err);
return next(null, resBody); // let it pass without the extra appends. No need to block the response
}

// Assign the extras to the services
resBody.data.forEach(function (service) {
service.extras = _.find(extras, function (extra) {
return extra.service_id === service.id
}) || {}
})
return next(null, resBody);
});
},
afterFetch: function(req, data, next) {

var connectionId = req.connection.id;
var entityId = req.path.split("/").filter(function (e) {
return e;
})[1];

sails.models.kongservices.findOne({
kong_node_id: connectionId,
service_id: entityId
}, function (err, extras) {
if(err) {
sails.log.error(err);
return next(err);
}

if(!extras) return next(null, data);

// Add the extras attr
data.extras = extras;
return next(null, data);
});
},
afterCreate: function(req, data, konga_extras, next) {

var connectionId = req.connection.id;
var entityId = data.id;

sails.models.kongservices.create(_.merge({
kong_node_id: connectionId,
service_id: entityId
}, konga_extras), function (err, extras) {
if(err) {
sails.log.error(err);
return next(err);
}

// Add the extras attr
data.extras = extras;
return next(null, data);
});
},
afterDelete: function(req, next) {

var connectionId = req.connection.id;
// The path must be of type /kong/<entityName>/<entityId>
var entityId = req.path.replace("/kong","").split("/").filter(function (e) {
return e;
})[1];

sails.models.kongservices.destroy({
kong_node_id: connectionId,
service_id: entityId
},function (err) {
if(err) {
sails.log.error("Failed to remove kong service extras",err);
return next(err);
}

return next();
})
},
}
}


Expand Down

0 comments on commit 41417a0

Please sign in to comment.