From b96d66591f36f9b733a64f06d55b1e3617a1af11 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sat, 26 Mar 2016 17:33:22 +0300 Subject: [PATCH] + hudozka-data package; /api/v1/feed -> /api/v1/timeline; other updates --- modules/hudozka-data/.gitignore | 3 +++ modules/hudozka-data/package.json | 18 ++++++++++++++ .../data => modules/hudozka-data/src}/Data.js | 16 ++++++------- modules/hudozka-data/src/DataManager.js | 7 ++++++ .../hudozka-data/src/Timeline.js | 14 +---------- modules/hudozka-data/src/index.js | 2 ++ package.json | 3 +++ services/Instagram/src/api/Feed.js | 2 +- src/api/feed.js | 21 ++++++++-------- src/api/index.js | 4 ++-- src/data/index.js | 5 ---- src/index.js | 24 ++++++++++++------- src/io.js | 14 +++++------ src/server.js | 10 ++++++-- 14 files changed, 84 insertions(+), 59 deletions(-) create mode 100644 modules/hudozka-data/.gitignore create mode 100644 modules/hudozka-data/package.json rename {src/data => modules/hudozka-data/src}/Data.js (53%) create mode 100644 modules/hudozka-data/src/DataManager.js rename src/data/Feed.js => modules/hudozka-data/src/Timeline.js (83%) create mode 100644 modules/hudozka-data/src/index.js delete mode 100644 src/data/index.js diff --git a/modules/hudozka-data/.gitignore b/modules/hudozka-data/.gitignore new file mode 100644 index 00000000..de1c106a --- /dev/null +++ b/modules/hudozka-data/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +lib/ +.idea/ diff --git a/modules/hudozka-data/package.json b/modules/hudozka-data/package.json new file mode 100644 index 00000000..e16e97cf --- /dev/null +++ b/modules/hudozka-data/package.json @@ -0,0 +1,18 @@ +{ + "name": "hudozka-data", + "private": true, + "version": "0.0.1", + "description": "", + "main": "lib/index.js", + "scripts": { + "compile": "babel --presets es2015,stage-0 src/ -d lib/", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "devDependencies": { + "babel-cli": "^6.6.5", + "babel-preset-es2015": "^6.6.0", + "babel-preset-stage-0": "^6.5.0" + } +} diff --git a/src/data/Data.js b/modules/hudozka-data/src/Data.js similarity index 53% rename from src/data/Data.js rename to modules/hudozka-data/src/Data.js index f19abc00..1fb6ee14 100644 --- a/src/data/Data.js +++ b/modules/hudozka-data/src/Data.js @@ -1,23 +1,21 @@ import {EventEmitter} from 'events'; -import {collection} from '../core/db'; export default class Data extends EventEmitter { - constructor(storeName) { + constructor(store) { super(); this.save = true; - this.name = storeName; - this.store = collection(storeName); + this.store = store; } - - update(document){ + + update(document) { this.emit('update', document); } - - disableSaving(){ + + disableSaving() { this.save = false; } - enableSaving(){ + enableSaving() { this.save = true; } } diff --git a/modules/hudozka-data/src/DataManager.js b/modules/hudozka-data/src/DataManager.js new file mode 100644 index 00000000..d9064701 --- /dev/null +++ b/modules/hudozka-data/src/DataManager.js @@ -0,0 +1,7 @@ +import Timeline from './Timeline'; + +export default class DataManager { + constructor(stores) { + this.timeline = new Timeline(stores.timeline); + } +} diff --git a/src/data/Feed.js b/modules/hudozka-data/src/Timeline.js similarity index 83% rename from src/data/Feed.js rename to modules/hudozka-data/src/Timeline.js index 608fca24..21ab1de0 100644 --- a/src/data/Feed.js +++ b/modules/hudozka-data/src/Timeline.js @@ -1,10 +1,6 @@ import Data from './Data'; -export default class Feed extends Data { - constructor() { - super('timeline'); - } - +export default class Timeline extends Data { add(post) { let savePost = async(post) => { const id = post.id; @@ -28,12 +24,4 @@ export default class Feed extends Data { return add(post); } - - read(id) { - - } - - portion() { - - } } diff --git a/modules/hudozka-data/src/index.js b/modules/hudozka-data/src/index.js new file mode 100644 index 00000000..2986bef3 --- /dev/null +++ b/modules/hudozka-data/src/index.js @@ -0,0 +1,2 @@ +import DataManager from './DataManager'; +export default DataManager; diff --git a/package.json b/package.json index 48c2aa7b..8ff63e71 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "babel-preset-stage-0": "^6.5.0", "co": "^4.4.0", "ejson": "^2.1.2", + "hudozka-data": "file:modules/hudozka-data", "instagram-node": "^0.5.1", "koa": "^2.0.0-alpha.3", "koa-bodyparser": "^3.0.0", @@ -33,7 +34,9 @@ "koa-etag": "^2.0.0", "koa-helmet": "^1.0.0", "koa-logger": "^2.0.0", + "koa-mount": "^2.0.0", "koa-prerender": "^1.0.3", + "koa-query": "0.0.2", "koa-route": "^3.1.0", "koa-static": "^3.0.0", "mongodb": "^2.1.11", diff --git a/services/Instagram/src/api/Feed.js b/services/Instagram/src/api/Feed.js index 63bdad55..e7466f81 100644 --- a/services/Instagram/src/api/Feed.js +++ b/services/Instagram/src/api/Feed.js @@ -2,6 +2,6 @@ import EndPoint from './EndPoint'; export default class Feed extends EndPoint { add(post) { - return this.request.request('PUT', '/feed', post); + return this.request.request('PUT', '/timeline', post); } } diff --git a/src/api/feed.js b/src/api/feed.js index 59d653af..947dac85 100644 --- a/src/api/feed.js +++ b/src/api/feed.js @@ -2,26 +2,25 @@ import {put} from 'koa-route'; import compose from 'koa-compose'; import {ensureServiceAuth} from '../middlewares/ensureServiceAuth'; import {ejson} from '../middlewares/ejson'; -import {queryValue, booleanify} from '../middlewares/queryValue'; +import query from 'koa-query'; +import {toBoolean} from 'koa-query'; -import data from '../data'; - -export function add() { - return put('/api/v1/feed', compose([ +export function add(auth, store) { + return put('/timeline', compose([ ensureServiceAuth(), ejson(), - queryValue({ - save: booleanify() + query({ + save: toBoolean() }), async(ctx) => { let doSave = ctx.request.query.save || true; let post = ctx.request.body; try { - if(!doSave) data.feed.disableSaving(); - let result = await data.feed.add(post); - if(!doSave) data.feed.enableSaving(); - + if(!doSave) store.timeline.disableSaving(); + let result = await store.timeline.add(post); + if(!doSave) store.timeline.enableSaving(); + ctx.body = result; } catch (e) { ctx.status = 400; diff --git a/src/api/index.js b/src/api/index.js index 0a031397..3ed872e0 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -1,8 +1,8 @@ import {add as feedAdd} from './feed'; import compose from 'koa-compose'; -export default function () { +export default function (auth, store) { return compose([ - feedAdd() + feedAdd(auth, store) ]); }; diff --git a/src/data/index.js b/src/data/index.js deleted file mode 100644 index 535d122a..00000000 --- a/src/data/index.js +++ /dev/null @@ -1,5 +0,0 @@ -import Feed from './Feed'; - -export default { - feed: new Feed() -}; diff --git a/src/index.js b/src/index.js index c649e3dc..f7d84ec9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,21 +1,29 @@ import 'babel-polyfill'; import config from './config'; -import {connect} from './core/db'; +import {connect, collection} from './core/db'; +import io from './io'; +import server from './server'; + +import DataManager from 'hudozka-data'; +// var DataManager = require('hudozka-data').default; function main() { - async function loop(){ + async function loop() { await connect(config.db.uri); - try{ - var server = require('./server').default; - var io = require('./io').default; + try { + let data = new DataManager({ + timeline: collection('timeline') + }); + + let app = server(data); + io(app, data); - var app = server(); - io(app); app.listen(config.port); - }catch(e){ + } catch (e) { console.error(e.stack); + return; } console.log(`Start listening ${config.port}`); diff --git a/src/io.js b/src/io.js index f2b578eb..65394931 100644 --- a/src/io.js +++ b/src/io.js @@ -1,7 +1,6 @@ import socketIO from 'socket.io'; -import data from './data'; -export default function(server) { +export default function(server, store) { let io = socketIO(server); io.on('connection', socket => { // console.log('a user connected'); @@ -18,11 +17,10 @@ export default function(server) { }); let transfer = name => i => io.emit(name, i); - let feed = transfer('feed'); + let timeline = transfer('timeline'); - data.feed.on('update', feed); - - data.feed.on('update', i => { - console.log(`io update ${JSON.stringify(i, null, '\t')}`); - }); + store.timeline.on('update', timeline); + // store.timeline.on('update', i => { + // console.log(`io update ${JSON.stringify(i, null, '\t')}`); + // }); } diff --git a/src/server.js b/src/server.js index 39aedfde..940eec67 100644 --- a/src/server.js +++ b/src/server.js @@ -8,6 +8,7 @@ import conditional from 'koa-conditional-get'; import etag from 'koa-etag'; import prerender from 'koa-prerender'; import helmet from 'koa-helmet'; +import mount from 'koa-mount'; import bodyParser from 'koa-bodyparser'; import config from './config'; @@ -15,11 +16,12 @@ import {redirectionTable} from './config'; import {routes, queryObject} from './routes'; import api from './api'; import {redirect} from './routes/redirect'; +import {checkAuth} from './core/service'; const dirPublic = path.join(__dirname, '../public'); const dirTemplates = path.join(__dirname, 'templates'); -export default function(){ +export default function(store){ const $ = convert; const app = new Koa(); @@ -31,7 +33,7 @@ export default function(){ } })); app.use(logger()); - app.use(api()); + app.use(apis(store)); app.use($(conditional())); app.use($(etag())); app.use(prerenderRmFragment()); @@ -46,6 +48,10 @@ export default function(){ return Server(app.callback()); } +function apis(store){ + return mount('/api/v1', api(checkAuth, store)); +} + function prerenderRmFragment(){ return async (ctx, next) => { await next();