Commit 241f4753 authored by Evgeniy Zaitsev's avatar Evgeniy Zaitsev
Browse files

v.1.0.1

parent 0c82b8b2
{ {
"extends": ["google"], "extends": [
"google"
],
"rules": { "rules": {
"max-len": [2, 120, 2], "max-len": [
"indent": [2, "tab",{ 2,
"SwitchCase": 1 120,
}], 2
"camelcase": [2, { ],
"properties": "never" "indent": [
}], 2,
"tab",
{
"SwitchCase": 1
}
],
"camelcase": [
2,
{
"properties": "never"
}
],
"require-jsdoc": 0, "require-jsdoc": 0,
"no-debugger": 1 "no-debugger": 1,
"no-invalid-this": 0
}, },
"env": { "env": {
"es6": true, "es6": true,
......
...@@ -45,7 +45,9 @@ class SDK { ...@@ -45,7 +45,9 @@ class SDK {
this.server.context.db = mysql.createConnection(this.config.mySql); this.server.context.db = mysql.createConnection(this.config.mySql);
this.server.context.logger = this.logger; this.server.context.logger = this.logger;
this.server.context.config = this.config; this.server.context.config = this.config;
this.server.context.throwResponseErrorIfNeed = throwResponseErrorIfNeed.bind(this.server.context); this.server.context.throwResponseErrorIfNeed = throwResponseErrorIfNeed.bind(
this.server.context
);
Promise.promisifyAll(this.server.context.db); Promise.promisifyAll(this.server.context.db);
} }
...@@ -61,53 +63,92 @@ class SDK { ...@@ -61,53 +63,92 @@ class SDK {
} }
fetchPrivateLatestResultsByEventFeedUUID(feedEndpoint, eventFeedUUID) { fetchPrivateLatestResultsByEventFeedUUID(feedEndpoint, eventFeedUUID) {
this.logger.log(__filename, 'fetch private latest result by event feed uuid', feedEndpoint, eventFeedUUID); this.logger.log(
__filename,
'fetch private latest result by event feed uuid',
feedEndpoint,
eventFeedUUID
);
return this._fetchPrivateFeed('privateLatestResults', feedEndpoint, eventFeedUUID); return this._fetchPrivateFeed('privateLatestResults', feedEndpoint, eventFeedUUID);
} }
fetchPrivateAllPublishedElementsByEventFeedUUID(feedEndpoint, eventFeedUUID) { fetchPrivateAllPublishedElementsByEventFeedUUID(feedEndpoint, eventFeedUUID) {
this.logger.log(__filename, 'fetch private all publish elements by event feed uuid', feedEndpoint, eventFeedUUID); this.logger.log(
__filename,
'fetch private all publish elements by event feed uuid',
feedEndpoint,
eventFeedUUID
);
return this._fetchPrivateFeed('privateAllPublishedElements', feedEndpoint, eventFeedUUID); return this._fetchPrivateFeed('privateAllPublishedElements', feedEndpoint, eventFeedUUID);
} }
fetchPrivateAllElementsByEventFeedUUID(feedEndpoint, eventFeedUUID) { fetchPrivateAllElementsByEventFeedUUID(feedEndpoint, eventFeedUUID) {
this.logger.log(__filename, 'fetch private all elements by event feed uuid', feedEndpoint, eventFeedUUID); this.logger.log(
__filename,
'fetch private all elements by event feed uuid',
feedEndpoint,
eventFeedUUID
);
return this._fetchPrivateFeed('privateAllElements', feedEndpoint, eventFeedUUID); return this._fetchPrivateFeed('privateAllElements', feedEndpoint, eventFeedUUID);
} }
fetchPrivateLatestResultsByEventId(feedEndpoint, projectFeedUUID, eventId) { fetchPrivateLatestResultsByEventId(feedEndpoint, projectFeedUUID, eventId) {
this.logger.log(__filename, 'fetch private latest result by event uuid', this.logger.log(
feedEndpoint, projectFeedUUID, eventId __filename,
); 'fetch private latest result by event uuid',
feedEndpoint,
return this._fetchPrivateListingsAndGetEventFeedUUID(feedEndpoint, projectFeedUUID, eventId) projectFeedUUID,
.then(eventFeedUUID => this.fetchPrivateLatestResultsByEventFeedUUID(feedEndpoint, eventFeedUUID)); eventId
);
return this._fetchPrivateListingsAndGetEventFeedUUID(
feedEndpoint,
projectFeedUUID,
eventId
).then((eventFeedUUID) =>
this.fetchPrivateLatestResultsByEventFeedUUID(feedEndpoint, eventFeedUUID));
} }
fetchPrivateAllPublishedElementsByEventId(feedEndpoint, projectFeedUUID, eventId) { fetchPrivateAllPublishedElementsByEventId(feedEndpoint, projectFeedUUID, eventId) {
this.logger.log(__filename, 'fetch private all publish elements by event uuid', this.logger.log(
feedEndpoint, projectFeedUUID, eventId __filename,
); 'fetch private all publish elements by event uuid',
feedEndpoint,
return this._fetchPrivateListingsAndGetEventFeedUUID(feedEndpoint, projectFeedUUID, eventId) projectFeedUUID,
.then(eventFeedUUID => this.fetchPrivateAllPublishedElementsByEventFeedUUID(feedEndpoint, eventFeedUUID)); eventId
);
return this._fetchPrivateListingsAndGetEventFeedUUID(
feedEndpoint,
projectFeedUUID,
eventId
).then((eventFeedUUID) =>
this.fetchPrivateAllPublishedElementsByEventFeedUUID(feedEndpoint, eventFeedUUID));
} }
fetchPrivateAllElementsByEventId(feedEndpoint, projectFeedUUID, eventId) { fetchPrivateAllElementsByEventId(feedEndpoint, projectFeedUUID, eventId) {
this.logger.log(__filename, 'fetch private all elements by event uuid', this.logger.log(
feedEndpoint, projectFeedUUID, eventId __filename,
); 'fetch private all elements by event uuid',
feedEndpoint,
return this._fetchPrivateListingsAndGetEventFeedUUID(feedEndpoint, projectFeedUUID, eventId) projectFeedUUID,
.then(eventFeedUUID => this.fetchPrivateAllElementsByEventFeedUUID(feedEndpoint, eventFeedUUID)); eventId
);
return this._fetchPrivateListingsAndGetEventFeedUUID(
feedEndpoint,
projectFeedUUID,
eventId
).then((eventFeedUUID) =>
this.fetchPrivateAllElementsByEventFeedUUID(feedEndpoint, eventFeedUUID));
} }
* _run() { * _run() {
yield * connectToMySql.call(this.server.context); yield* connectToMySql.call(this.server.context);
yield * createTables.call(this.server.context); yield* createTables.call(this.server.context);
this._createHttp(); this._createHttp();
this._createHttps(); this._createHttps();
...@@ -116,7 +157,10 @@ class SDK { ...@@ -116,7 +157,10 @@ class SDK {
_createHttp() { _createHttp() {
http.createServer(this.server.callback()).listen(this.config.httpPort); http.createServer(this.server.callback()).listen(this.config.httpPort);
this.server.context.logger.log(__filename, `HTTP is listening on port: ${this.config.httpPort}`); this.server.context.logger.log(
__filename,
`HTTP is listening on port: ${this.config.httpPort}`
);
} }
_createHttps() { _createHttps() {
...@@ -126,12 +170,15 @@ class SDK { ...@@ -126,12 +170,15 @@ class SDK {
const options = { const options = {
key: fs.readFileSync(this.config.ssl.keyFileName), key: fs.readFileSync(this.config.ssl.keyFileName),
cert: fs.readFileSync(this.config.ssl.certFileName) cert: fs.readFileSync(this.config.ssl.certFileName),
}; };
https.createServer(options, this.server.callback()).listen(this.config.httpsPort); https.createServer(options, this.server.callback()).listen(this.config.httpsPort);
this.server.context.logger.log(__filename, `HTTPS is listening on port: ${this.config.httpsPort}`); this.server.context.logger.log(
__filename,
`HTTPS is listening on port: ${this.config.httpsPort}`
);
} }
_fetchPrivateFeed(feedName, feedEndpoint, feedUUID) { _fetchPrivateFeed(feedName, feedEndpoint, feedUUID) {
...@@ -139,16 +186,16 @@ class SDK { ...@@ -139,16 +186,16 @@ class SDK {
this.logger.log(__filename, 'fetch private feed', url); this.logger.log(__filename, 'fetch private feed', url);
return fetch(url).then(res => res.json()); return fetch(url).then((res) => res.json());
} }
_fetchPrivateListingsAndGetEventFeedUUID(feedEndpoint, projectFeedUUID, eventId) { _fetchPrivateListingsAndGetEventFeedUUID(feedEndpoint, projectFeedUUID, eventId) {
return this.fetchPrivateListings(feedEndpoint, projectFeedUUID) return this.fetchPrivateListings(feedEndpoint, projectFeedUUID).then((listings) =>
.then(listings => SDK._getEventFeedUUIDFromListings(listings, eventId)); SDK._getEventFeedUUIDFromListings(listings, eventId));
} }
static _getEventFeedUUIDFromListings(listings, eventId) { static _getEventFeedUUIDFromListings(listings, eventId) {
const event = listings.events.find(event => event.id === eventId); const event = listings.events.find((event) => event.id === eventId);
if (!event) { if (!event) {
throw new Error('event is no found in listings'); throw new Error('event is no found in listings');
......
module.exports = { module.exports = {
INSTANCES: 'instances', INSTANCES: 'instances',
PROJECTS: 'projects' PROJECTS: 'projects',
}; };
'use strict'; 'use strict';
function * connectToMySql() { function* connectToMySql() {
try { try {
yield this.db.connectAsync(); yield this.db.connectAsync();
......
'use strict'; 'use strict';
function * createTables() { function* createTables() {
try { try {
yield this.db.queryAsync(` yield this.db.queryAsync(
`
CREATE TABLE IF NOT EXISTS \`${this.config.mySql.database}\`.\`${this.tablesNames.INSTANCES}\` ( CREATE TABLE IF NOT EXISTS \`${this.config.mySql.database}\`.\`${this.tablesNames.INSTANCES}\` (
\`id\` CHAR(36) NOT NULL, \`id\` CHAR(36) NOT NULL,
\`name\` CHAR(255) NOT NULL, \`name\` CHAR(255) NOT NULL,
...@@ -11,9 +12,11 @@ function * createTables() { ...@@ -11,9 +12,11 @@ function * createTables() {
PRIMARY KEY (\`id\`), PRIMARY KEY (\`id\`),
UNIQUE INDEX \`id_UNIQUE\` (\`id\` ASC) UNIQUE INDEX \`id_UNIQUE\` (\`id\` ASC)
) )
`); `
);
yield this.db.queryAsync(` yield this.db.queryAsync(
`
CREATE TABLE IF NOT EXISTS \`${this.config.mySql.database}\`.\`${this.tablesNames.PROJECTS}\` ( CREATE TABLE IF NOT EXISTS \`${this.config.mySql.database}\`.\`${this.tablesNames.PROJECTS}\` (
\`id\` CHAR(36) NOT NULL, \`id\` CHAR(36) NOT NULL,
\`name\` CHAR(255) NOT NULL, \`name\` CHAR(255) NOT NULL,
...@@ -22,7 +25,8 @@ function * createTables() { ...@@ -22,7 +25,8 @@ function * createTables() {
PRIMARY KEY (\`id\`), PRIMARY KEY (\`id\`),
UNIQUE INDEX \`id_UNIQUE\` (\`id\` ASC) UNIQUE INDEX \`id_UNIQUE\` (\`id\` ASC)
) )
`); `
);
this.logger.log(__filename, 'tables created successfully'); this.logger.log(__filename, 'tables created successfully');
} catch (err) { } catch (err) {
......
...@@ -5,11 +5,4 @@ const debug = require('debug'); ...@@ -5,11 +5,4 @@ const debug = require('debug');
const log = debug('service-sdk:log'); const log = debug('service-sdk:log');
const error = debug('service-sdk:error'); const error = debug('service-sdk:error');
module.exports = { module.exports = {log, error};
log() {
log.apply(null, arguments);
},
error() {
error.apply(null, arguments);
}
};
...@@ -2,12 +2,13 @@ ...@@ -2,12 +2,13 @@
const urlJoin = require('url-join'); const urlJoin = require('url-join');
const privateFeed = name => const privateFeed = (name) =>
(feedEndpoint, version, feedUUID) => urlJoin(feedEndpoint, 'feeds', name, version, `${feedUUID}.json`); (feedEndpoint, version, feedUUID) =>
urlJoin(feedEndpoint, 'feeds', name, version, `${feedUUID}.json`);
module.exports = { module.exports = {
privateListings: privateFeed('listings'), privateListings: privateFeed('listings'),
privateLatestResults: privateFeed('latest_results'), privateLatestResults: privateFeed('latest_results'),
privateAllPublishedElements: privateFeed('all_published'), privateAllPublishedElements: privateFeed('all_published'),
privateAllElements: privateFeed('all') privateAllElements: privateFeed('all'),
}; };
...@@ -16,7 +16,7 @@ function serialize(obj, prefix) { ...@@ -16,7 +16,7 @@ function serialize(obj, prefix) {
} }
} }
return res.join("&"); return res.join('&');
} }
module.exports = serialize; module.exports = serialize;
const parse = require('co-body'); const parse = require('co-body');
function * bodyParser(next) { function* bodyParser(next) {
this.request.bodyRow = yield parse.text(this.request); this.request.bodyRow = yield parse.text(this.request);
try { try {
...@@ -13,4 +13,3 @@ function * bodyParser(next) { ...@@ -13,4 +13,3 @@ function * bodyParser(next) {
} }
module.exports = bodyParser; module.exports = bodyParser;
function * checkEventId(next) { function* checkEventId(next) {
const eventId = this.checkParams('eventId').isUUID().value; const eventId = this.checkParams('eventId').isUUID().value;
this.logger.log(__filename, 'GET', 'event id:', eventId); this.logger.log(__filename, 'GET', 'event id:', eventId);
......
function * checkInstanceId(next) { function* checkInstanceId(next) {
const instanceId = this.checkParams('instanceId').isUUID().value; const instanceId = this.checkParams('instanceId').isUUID().value;
this.logger.log(__filename, 'instance id:', instanceId); this.logger.log(__filename, 'instance id:', instanceId);
......
function * checkInstanceId(next) { function* checkInstanceId(next) {
const projectId = this.checkParams('projectId').isUUID().value; const projectId = this.checkParams('projectId').isUUID().value;
this.logger.log(__filename, 'project id:', projectId); this.logger.log(__filename, 'project id:', projectId);
......
...@@ -4,7 +4,7 @@ const sha1 = require('sha1'); ...@@ -4,7 +4,7 @@ const sha1 = require('sha1');
const urlJoin = require('url-join'); const urlJoin = require('url-join');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
function * checkSessionToken(next) { function* checkSessionToken(next) {
const sessionToken = this.checkQuery('lvis_at').notEmpty().value; const sessionToken = this.checkQuery('lvis_at').notEmpty().value;
this.logger.log(__filename, 'session token', sessionToken); this.logger.log(__filename, 'session token', sessionToken);
...@@ -36,7 +36,7 @@ function fetchUser(sessionToken) { ...@@ -36,7 +36,7 @@ function fetchUser(sessionToken) {
this.logger.log(__filename, 'fetch user url:', url, 'headers:', headers); this.logger.log(__filename, 'fetch user url:', url, 'headers:', headers);
return fetch(url, {headers: headers}).then(res => res.json()); return fetch(url, {headers: headers}).then((res) => res.json());
} }
module.exports = checkSessionToken; module.exports = checkSessionToken;
const sha1 = require('sha1'); const sha1 = require('sha1');
function * checkToken(next) { function* checkToken(next) {
const salt = this.checkHeader('x-lvis-salt').notBlank().value; const salt = this.checkHeader('x-lvis-salt').notBlank().value;
const signature = this.checkHeader('x-lvis-signature').notBlank().value; const signature = this.checkHeader('x-lvis-signature').notBlank().value;
const querystring = this.request.querystring ? '?' + this.request.querystring : ''; const querystring = this.request.querystring ? '?' + this.request.querystring : '';
......
function * checkTabId(next) { function* checkTabId(next) {
const tabId = this.checkParams('tabId').notEmpty().value; const tabId = this.checkParams('tabId').notEmpty().value;
this.logger.log(__filename, 'GET', 'tab id:', tabId); this.logger.log(__filename, 'GET', 'tab id:', tabId);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
const ResponseError = require('../lib/ResponseError'); const ResponseError = require('../lib/ResponseError');
function * errors(next) { function* errors(next) {
try { try {
yield next; yield next;
} catch (err) { } catch (err) {
...@@ -10,7 +10,7 @@ function * errors(next) { ...@@ -10,7 +10,7 @@ function * errors(next) {
if (err instanceof Error) { if (err instanceof Error) {
this.body = { this.body = {
message: err.message message: err.message,
}; };
this.logger.error(err); this.logger.error(err);
......
function * protocol(next) { function* protocol(next) {
this.state.protocol = this.config.forceHttps ? 'https' : this.protocol; this.state.protocol = this.config.forceHttps ? 'https' : this.protocol;
this.logger.log(__filename, 'protocol:', this.state.protocol); this.logger.log(__filename, 'protocol:', this.state.protocol);
...@@ -7,4 +7,3 @@ function * protocol(next) { ...@@ -7,4 +7,3 @@ function * protocol(next) {
} }
module.exports = protocol; module.exports = protocol;
{ {
"name": "service-sdk", "name": "service-sdk",
"version": "1.0.0", "version": "1.0.1",
"private": true, "private": true,
"description": "Service SDK for LViS", "description": "Service SDK for LViS",
"keywords": [], "keywords": [],
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
}, },
"scripts": { "scripts": {
"start": "node app.js", "start": "node app.js",
"lint": "eslint ./**/*.js", "lint": "eslint ./**/*.js ./*.js",
"copy-to-releases": "cpy constants/* ../service-sdk-releases/constants && cpy lib/* ../service-sdk-releases/lib && cpy middlewares/* ../service-sdk-releases/middlewares && cpy router/* ../service-sdk-releases/router && cpy .editorconfig .eslintignore .eslintrc .gitignore app.js package.json ../service-sdk-releases" "copy-to-releases": "cpy constants/* ../service-sdk-releases/constants && cpy lib/* ../service-sdk-releases/lib && cpy middlewares/* ../service-sdk-releases/middlewares && cpy router/* ../service-sdk-releases/router && cpy .editorconfig .eslintignore .eslintrc .gitignore app.js package.json ../service-sdk-releases"
}, },
"devDependencies": { "devDependencies": {
......
'use strict'; 'use strict';
function * get() { function* get() {
this.body = yield this.config.analyticsCallback(this.state.instance, this.state.project, this.state.eventId); this.body = yield this.config.analyticsCallback(
this.state.instance,
this.state.project,
this.state.eventId
);
} }
module.exports = {get: get}; module.exports = {get: get};
'use strict';
function* get() {
const getAssets = this.config.assetsCallback;
const assets = {
endpoint: this.config.wsEndpoint,
};
if (getAssets) {
const customAssets = yield getAssets(
this.state.instance,
this.state.project,
this.state.eventId
);
Object.assign({}, customAssets, assets);
}
this.body = assets;
}
module.exports = {get: get};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment