Skip to content

UPDATE: adding custom gateway #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions ironhead/app/gateway/article.js

This file was deleted.

27 changes: 0 additions & 27 deletions ironhead/app/hooks/gateway/init.js

This file was deleted.

13 changes: 13 additions & 0 deletions ironhead/app/hooks/httpClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const path = require('path');
const httpClient = require('../../lib/package/gateway/httpClient');

const CONFIG_PATH = path.join(__dirname, '../../config/http-client');

module.exports = async (app, cb) => {
try {
await httpClient.init(CONFIG_PATH);
cb();
} catch (e) {
cb(e);
}
};
6 changes: 4 additions & 2 deletions ironhead/app/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const init = require('./init');
const httpClient = require('./httpClient');
const redis = require('./redis');

const hooks = [
init
redis,
httpClient
];

module.exports = hooks;
13 changes: 0 additions & 13 deletions ironhead/app/hooks/init.js

This file was deleted.

10 changes: 10 additions & 0 deletions ironhead/app/hooks/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { init } = require('../../lib/package/adapter/redis');

module.exports = async (app, cb) => {
try {
await init();
cb();
} catch (e) {
cb(e);
}
};
16 changes: 16 additions & 0 deletions ironhead/app/modules/story/gatewayConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
exports.article = {
options: {
server: 'api',
method: 'get',
endPoint: '/v2/everything?domains=wsj.com,nytimes.com'
}
};

exports.gateway = () => (
{
ttl: 60,
type: 'rest',
cache: true,
configKeys: ['article']
}
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const gateway = require('../../../gateway/article');
const { gateway } = require('./gatewayConfig');

module.exports = {
domain: 'article',
Expand Down
2 changes: 1 addition & 1 deletion ironhead/app/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const article = require('../modules/story/routes/article');
const article = require('../modules/story/route');

const routes = [
article
Expand Down
2 changes: 2 additions & 0 deletions ironhead/config/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const session = require('./session');
const static = require('./static');
const gateway = require('./gateway');

module.exports = {
static,
gateway,
...session
};
11 changes: 0 additions & 11 deletions ironhead/config/ease-config/ease/getConfig.js

This file was deleted.

3 changes: 3 additions & 0 deletions ironhead/config/gateway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const gateway = require('../lib/package/gateway');

module.exports = gateway;
5 changes: 5 additions & 0 deletions ironhead/config/http-client/ease/getConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { article } = require('../../../app/modules/story/gatewayConfig');

module.exports = {
article
};
28 changes: 28 additions & 0 deletions ironhead/lib/package/adapter/redis/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Redis = require('ioredis');
Redis.Promise = require('bluebird')

let connection;

module.exports = {
init() {
if (connection) {
return Promise.resolve(connection);
}
return new Promise((resolve, reject) => {
connection = new Redis();
connection.set('testRedisConnection', 'test data');
connection.get('testRedisConnection', function (err, data) {
if (err) {
reject(err);
} else {
if (data === 'test data') {
resolve(connection);
}
}
});
});
},
getConnection() {
return connection;
}
};
22 changes: 22 additions & 0 deletions ironhead/lib/package/gateway/cacheClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { generateKey } = require('./util');
const { getConnection } = require('../adapter/redis');

const get = async (config) => {
const key = generateKey(JSON.stringify(config));
const redis = getConnection();
const data = await redis.get(key);

return data;
};

const set = (config, response) => {
const { ttl } = config;
const key = generateKey(JSON.stringify(config));
const redis = getConnection();
return setTimeout(() => redis.set(key, JSON.stringify(response), 'ex', ttl), 0);
};

module.exports = {
get,
set
};
59 changes: 59 additions & 0 deletions ironhead/lib/package/gateway/httpClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { fixtures, get } = require('../ease/orchestrator');
/**
*
* @description **init** is the wrapper over ease for initiating the fixtures
* path.
* <br />
*
* In order to send request using <strong> ease </strong>, we need to initialize
* the config path, which <strong> ease </strong> keeps it in memory.
* <br/>
*
* Application using <strong> knucklehead </strong>, needs to initialize it in the beginning
* since fixturing is asynchronous.
*
* @param { fixturesPath } fixturesPath - absolute config path needs to be provided.
* It's not a mandatory field otherwise it will bootstrap using the default EASE_CONFIG_PATH
* <br />
* @returns { Promise } Promise - ConfigPath promise is returned
*/
const init = (fixturesPath) => {
if (!fixturesPath) throw new Error('Fixtures Path for Gateway initialization not passed.');
const configPath = fixturesPath;
return fixtures(configPath);
};

const sanitize = (configKeys, response) => {
const isMulti = configKeys.length > 1;

if (isMulti) {
const parsedResponse = configKeys.reduce((acc, key) => {
const { [key]: { statusCode, body } } = response;
acc[key] = {
...body,
apiStatusCode: statusCode
};
return acc;
}, {});

return parsedResponse;
}
const [singleKeyName] = configKeys;
const { [singleKeyName]: { statusCode, body } } = response;
return {
...body,
apiStatusCode: statusCode
};
};

const request = async ({ configKeys, reqConfig, handlers }) => {
const httpResponse = await get(configKeys, reqConfig);
const parsedResponse = sanitize(configKeys, httpResponse);
const standardResponse = typeof handler === 'function' ? handler(parsedResponse) : parsedResponse;
return standardResponse;
}

module.exports = {
init,
request
};
30 changes: 30 additions & 0 deletions ironhead/lib/package/gateway/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const httpClient = require('./httpClient');
const cacheClient = require('./cacheClient');

module.exports = async (props) => {
const { reqConfig, ...gwConfig } = props;
const { configKeys, cache } = gwConfig;

try {
if (cache) {
const data = await cacheClient.get(gwConfig);

if (data) {
console.log('hit');
const response = JSON.parse(data);
return response;
}
console.log('miss');
const response = await httpClient.request(props);
cacheClient.set(gwConfig, response);
return response;
} else {
const response = await httpClient.request(props);
return response;
}

} catch (e) {
throw e;
}
};

7 changes: 7 additions & 0 deletions ironhead/lib/package/gateway/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const crypto = require('crypto');

exports.generateKey = (text) => {
const hash = crypto.createHash('md5');
hash.update(text);
return hash.digest('hex');
};
Loading