diff --git a/.gitignore b/.gitignore index 05647c85..7f42296e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ private*.json /node_modules /bower_components +/modules/hudozka-api-v1/lib /public /build /out diff --git a/install.sh b/install.sh index 5ac2b353..25e84113 100755 --- a/install.sh +++ b/install.sh @@ -12,6 +12,9 @@ gunzip -c ${build_file} | tar xopf - #rm build.tar npm install --production --loglevel error +npm install ./modules/hudozka-api-v1/ +npm install ./modules/hudozka-data/ +npm install ./modules/hudozka-middlewares/ #starting www if forever restart ${app}; then diff --git a/modules/hudozka-api-v1/lib/api/timeline.js b/modules/hudozka-api-v1/lib/api/timeline.js deleted file mode 100644 index 7ad6c756..00000000 --- a/modules/hudozka-api-v1/lib/api/timeline.js +++ /dev/null @@ -1,74 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.add = add; - -var _koaRoute = require('koa-route'); - -var _koaCompose = require('koa-compose'); - -var _koaCompose2 = _interopRequireDefault(_koaCompose); - -var _hudozkaMiddlewares = require('hudozka-middlewares'); - -var _hudozkaMiddlewares2 = _interopRequireDefault(_hudozkaMiddlewares); - -var _koaQuery = require('koa-query'); - -var _koaQuery2 = _interopRequireDefault(_koaQuery); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } - -function add(auth, data) { - var _this = this; - - return (0, _koaRoute.put)('/timeline', (0, _koaCompose2.default)([_hudozkaMiddlewares2.default.ensureServiceAuth(auth), _hudozkaMiddlewares2.default.ejson(), (0, _koaQuery2.default)({ - save: (0, _koaQuery.toBoolean)() - }), function () { - var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(ctx) { - var doSave, post, result; - return regeneratorRuntime.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - doSave = ctx.request.query.save || true; - post = ctx.request.body; - _context.prev = 2; - - if (!doSave) data.timeline.disableSaving(); - _context.next = 6; - return data.timeline.add(post); - - case 6: - result = _context.sent; - - if (!doSave) data.timeline.enableSaving(); - - ctx.body = result; - _context.next = 15; - break; - - case 11: - _context.prev = 11; - _context.t0 = _context['catch'](2); - - ctx.status = 400; - ctx.body = _context.t0; - - case 15: - case 'end': - return _context.stop(); - } - } - }, _callee, _this, [[2, 11]]); - })); - - return function (_x) { - return ref.apply(this, arguments); - }; - }()])); -} \ No newline at end of file diff --git a/modules/hudozka-api-v1/src/api/timeline.js b/modules/hudozka-api-v1/src/api/timeline.js index ebe90280..b5fade6a 100644 --- a/modules/hudozka-api-v1/src/api/timeline.js +++ b/modules/hudozka-api-v1/src/api/timeline.js @@ -1,13 +1,13 @@ import {put} from 'koa-route'; import compose from 'koa-compose'; -import hm from 'hudozka-middlewares'; import query from 'koa-query'; import {toBoolean} from 'koa-query'; +import {ejson, ensureServiceAuth} from 'hudozka-middlewares'; export function add(auth, data) { return put('/timeline', compose([ - hm.ensureServiceAuth(auth), - hm.ejson(), + ensureServiceAuth(auth), + ejson(), query({ save: toBoolean() }), diff --git a/modules/hudozka-middlewares/src/ensureServiceAuth.js b/modules/hudozka-middlewares/src/ensureServiceAuth.js index 99c1bbf1..eb67ad7a 100644 --- a/modules/hudozka-middlewares/src/ensureServiceAuth.js +++ b/modules/hudozka-middlewares/src/ensureServiceAuth.js @@ -1,3 +1,4 @@ +// auth - fn that returns boolean export function ensureServiceAuth(auth) { return async (ctx, next) => { let service = ctx.request.headers['x-service-name']; @@ -10,3 +11,4 @@ export function ensureServiceAuth(auth) { } }; } + diff --git a/src/core/service.js b/src/core/service.js index 3a80519c..64795dad 100644 --- a/src/core/service.js +++ b/src/core/service.js @@ -1,8 +1,8 @@ import {instagramToken} from '../config'; -const services = { +export const services = { instagram: instagramToken }; -export const checkAuth = (name, token) => name in services && services[name] === token; - +export const checkAuth = (name, token) => name in services && services[name] === token; +export const authChecker = store => (name, token) => name in store && store[name] === token; \ No newline at end of file diff --git a/src/server.js b/src/server.js index 3c16cfa8..b0f8a628 100644 --- a/src/server.js +++ b/src/server.js @@ -16,7 +16,7 @@ import config from './config'; import {redirectionTable} from './config'; import {routes, queryObject} from './routes'; import {redirect} from './routes/redirect'; -import {checkAuth} from './core/service'; +import {services as serviceKeys, authChecker} from './core/service'; const dirPublic = path.join(__dirname, '../public'); const dirTemplates = path.join(__dirname, 'templates'); @@ -49,6 +49,7 @@ export default function(store){ } function apis(store){ + let checkAuth = authChecker(serviceKeys); return mount('/api/v1', api_v1(checkAuth, store)); }