Commit ec84ea1b authored by Evgeniy Zaitsev's avatar Evgeniy Zaitsev
Browse files

v0.0.3

parent e2b661cb
......@@ -4,45 +4,104 @@ const koa = require('koa');
const validate = require('koa-validate');
const Promise = require('bluebird');
const mysql = require('mysql');
const fetch = require('node-fetch');
const tablesNames = require('./constants/tablesNames');
const throwResponseErrorIfNeed = require('./lib/throwResponseErrorIfNeed');
const connectToMySql = require('./lib/connectToMySql');
const lvisEndpoints = require('./lib/lvisEndpoints');
const createTables = require('./lib/createTables');
const logger = require('./lib/logger');
const errors = require('./middlewares/errors');
const router = require('./router');
const app = koa();
class SDK {
constructor(config) {
this.server = koa();
this.config = config;
validate(app);
validate(this.server);
app.use(errors);
app.use(require('koa-bodyparser')());
app.use(router.routes());
this.server.use(errors);
this.server.use(require('koa-bodyparser')());
this.server.use(router.routes());
module.exports = Promise.coroutine(main);
this.server.env = this.config.env || 'production';
function * main(config) {
app.env = config.env || 'production';
this.server.context.tablesNames = tablesNames;
this.server.context.db = mysql.createConnection(this.config.mySql);
this.server.context.logger = logger;
this.server.context.config = this.config;
this.server.context.throwResponseErrorIfNeed = throwResponseErrorIfNeed.bind(this.server.context);
app.context.tablesNames = tablesNames;
Promise.promisifyAll(this.server.context.db);
}
app.context.db = mysql.createConnection(config.mySql);
app.context.logger = logger;
app.context.config = config;
app.context.throwResponseErrorIfNeed = throwResponseErrorIfNeed.bind(app.context);
run() {
return Promise.coroutine(this._run.bind(this))();
}
Promise.promisifyAll(app.context.db);
fetchPrivateListings(feedEndpoint, projectFeedUUID) {
return this._fetchPrivateFeed('privateListings', feedEndpoint, projectFeedUUID);
}
yield * connectToMySql.call(app.context);
yield * createTables.call(app.context);
fetchPrivateLatestResultsByEventFeedUUID(feedEndpoint, eventFeedUUID) {
return this._fetchPrivateFeed('privateLatestResults', feedEndpoint, eventFeedUUID);
}
app.listen(config.httpPort);
fetchPrivateAllPublishedElementsByEventFeedUUID(feedEndpoint, eventFeedUUID) {
return this._fetchPrivateFeed('privateAllPublishedElements', feedEndpoint, eventFeedUUID);
}
logger.log(__filename, `App is listening on port: ${config.httpPort}`);
fetchPrivateAllElementsByEventFeedUUID(feedEndpoint, eventFeedUUID) {
return this._fetchPrivateFeed('privateAllElements', feedEndpoint, eventFeedUUID);
}
return app;
fetchPrivateLatestResultsByEventId(feedEndpoint, projectFeedUUID, eventId) {
return this._fetchPrivateListingsAndGetEventFeedUUID(feedEndpoint, projectFeedUUID, eventId)
.then(eventFeedUUID => this.fetchPrivateLatestResultsByEventFeedUUID(feedEndpoint, eventFeedUUID));
}
fetchPrivateAllPublishedElementsByEventId(feedEndpoint, projectFeedUUID, eventId) {
return this._fetchPrivateListingsAndGetEventFeedUUID(feedEndpoint, projectFeedUUID, eventId)
.then(eventFeedUUID => this.fetchPrivateAllPublishedElementsByEventFeedUUID(feedEndpoint, eventFeedUUID));
}
fetchPrivateAllElementsByEventId(feedEndpoint, projectFeedUUID, eventId) {
return this._fetchPrivateListingsAndGetEventFeedUUID(feedEndpoint, projectFeedUUID, eventId)
.then(eventFeedUUID => this.fetchPrivateAllElementsByEventFeedUUID(feedEndpoint, eventFeedUUID));
}
* _run() {
yield * connectToMySql.call(this.server.context);
yield * createTables.call(this.server.context);
this.server.listen(this.config.httpPort);
logger.log(__filename, `App is listening on port: ${this.config.httpPort}`);
}
_fetchPrivateFeed(feedName, feedEndpoint, feedUUID) {
return fetch(lvisEndpoints[feedName](feedEndpoint, this.config.feedVersion, feedUUID))
.then(res => res.json());
}
_fetchPrivateListingsAndGetEventFeedUUID(feedEndpoint, projectFeedUUID, eventId) {
return this.fetchPrivateListings(feedEndpoint, projectFeedUUID)
.then(listings => SDK._getEventFeedUUIDFromListings(listings, eventId));
}
static _getEventFeedUUIDFromListings(listings, eventId) {
const event = listings.events.find(event => event.id === eventId);
if (!event) {
throw new Error('event is no found in listings');
}
return event.id;
}
}
module.exports = SDK;
'use strict';
const urlJoin = require('url-join');
const privateFeed = name =>
(feedEndpoint, version, feedUUID) => urlJoin(feedEndpoint, 'feeds', name, version, `${feedUUID}.json`);
module.exports = {
privateListings: privateFeed('listings'),
privateLatestResults: privateFeed('latest_results'),
privateAllPublishedElements: privateFeed('all_published'),
privateAllElements: privateFeed('all')
};
{
"name": "service-sdk",
"version": "0.0.2",
"version": "0.0.3",
"private": true,
"description": "Service SDK for LViS",
"keywords": [],
......@@ -16,7 +16,9 @@
"koa-router": "^5.4.0",
"koa-validate": "^1.0.7",
"mysql": "^2.11.1",
"sha1": "^1.1.1"
"node-fetch": "^1.6.3",
"sha1": "^1.1.1",
"url-join": "^1.1.0"
},
"scripts": {
"start": "node app.js",
......
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