data api with io
This commit is contained in:
1 parent
0ffa403b60
commit
aea2b8ff25
17 files changed
+159
-91
No files matched your search
+2
-1
@@ -33,9 +33,10 @@
|
||||
"koa-prerender": "^1.0.3",
|
||||
"koa-route": "^3.1.0",
|
||||
"koa-static": "^2.0.0",
|
||||
"mongodb": "^2.1.11",
|
||||
"mz": "^2.3.1",
|
||||
"promised-mongo": "git+https://git@github.com/tmshv/promised-mongo",
|
||||
"sitemap": "^1.5.0",
|
||||
"socket.io": "^1.4.5",
|
||||
"thunkify": "^2.1.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import {put} from 'koa-route';
|
||||
import compose from 'koa-compose';
|
||||
import {ensureServiceAuth} from '../middlewares/ensureServiceAuth';
|
||||
|
||||
import data from '../data';
|
||||
|
||||
export function add() {
|
||||
return put('/api/v1/feed', compose([
|
||||
ensureServiceAuth(),
|
||||
async(ctx) => {
|
||||
let post = ctx.request.body;
|
||||
|
||||
try {
|
||||
ctx.body = await data.feed.add(post);
|
||||
} catch (e) {
|
||||
ctx.status = 400;
|
||||
ctx.body = e;
|
||||
}
|
||||
}
|
||||
]));
|
||||
}
|
||||
File renamed without changes.
@@ -1,12 +0,0 @@
|
||||
require('babel-core/register');
|
||||
require('babel-polyfill');
|
||||
|
||||
var config = require("../config").default;
|
||||
var db = require("../core/db");
|
||||
var server = require("../server").server;
|
||||
|
||||
db.init(config.db.uri)
|
||||
.then(function () {
|
||||
var app = server();
|
||||
app.listen(config.port);
|
||||
});
|
||||
+2
-2
@@ -43,7 +43,7 @@ import UppercaseFirst from './filters/UppercaseFirst';
|
||||
import Bytes from './filters/Bytes';
|
||||
import Ext from './filters/Ext';
|
||||
|
||||
// import IOService from './services/io';
|
||||
import IOService from './services/io';
|
||||
import MenuService from './services/menu';
|
||||
import API from './services/api';
|
||||
|
||||
@@ -225,7 +225,7 @@ app.run(($location, $rootScope, $http) => {
|
||||
|
||||
[
|
||||
API,
|
||||
// IOService,
|
||||
IOService,
|
||||
MenuService,
|
||||
RemoveHashtags,
|
||||
RemoveNewline,
|
||||
|
||||
@@ -27,14 +27,13 @@ function postText(post){
|
||||
export default function(app) {
|
||||
app.component('timeline', {
|
||||
template: template,
|
||||
controller: function($timeout, usSpinnerService, api){
|
||||
controller: function($timeout, usSpinnerService, api, io){
|
||||
let skip = 0;
|
||||
let limit = 10;
|
||||
this.feed = [];
|
||||
|
||||
this.timelineUpdating = false;
|
||||
usSpinnerService.stop('timelineMore');
|
||||
//usSpinnerService.spin("timelineMore");
|
||||
|
||||
function toInstagram(post){
|
||||
//var author_id = personByInstagram(team.team, post.data.author);
|
||||
@@ -68,7 +67,6 @@ export default function(app) {
|
||||
else return post;
|
||||
});
|
||||
|
||||
//skip++;
|
||||
skip += limit;
|
||||
this.feed = this.feed.concat(feed2);
|
||||
this.showFooter = this.feed && this.feed.length;
|
||||
@@ -77,18 +75,16 @@ export default function(app) {
|
||||
|
||||
this.loadNext();
|
||||
|
||||
// io.on('post', params => {
|
||||
//let posts = params instanceof Array ? params : [params];
|
||||
//
|
||||
//let feed2 = posts.map(post => {
|
||||
// if(post.type == 'instagram') return toInstagram(post);
|
||||
// else return post;
|
||||
//});
|
||||
io.on('feed', feed => {
|
||||
// console.log(feed);
|
||||
|
||||
//this.$apply(() => {
|
||||
// this.feed = feed2.concat(this.feed);
|
||||
//});
|
||||
// });
|
||||
feed = feed instanceof Array ? feed : [feed];
|
||||
feed = feed.map(i => i.type === 'instagram' ? toInstagram(i) : i);
|
||||
|
||||
$timeout(()=>{
|
||||
this.feed = feed.concat(this.feed);
|
||||
}, 0);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
import {c} from '../core/db';
|
||||
|
||||
export async function getCollective(query={}, show={}) {
|
||||
let data = await c('collective').find(query, show);
|
||||
let data = await c('collective').find(query, show).toArray();
|
||||
if (!data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
+19
-41
@@ -1,46 +1,24 @@
|
||||
/**
|
||||
* Created by tmshv on 22/11/14.
|
||||
*/
|
||||
import {MongoClient, ObjectId} from 'mongodb';
|
||||
import co from 'co';
|
||||
|
||||
var mongo = require("promised-mongo");
|
||||
var ObjectId = mongo.ObjectId;
|
||||
export var db;
|
||||
|
||||
var client = null;
|
||||
export function connect(uri){
|
||||
return co(function*() {
|
||||
db = yield MongoClient.connect(uri);
|
||||
return db;
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function (uri) {
|
||||
//noinspection JSUnresolvedFunction
|
||||
return new Promise(function (resolve) {
|
||||
client = mongo(uri);
|
||||
resolve();
|
||||
});
|
||||
},
|
||||
export function collection(name){
|
||||
return db.collection(name);
|
||||
}
|
||||
|
||||
close: function () {
|
||||
client.close();
|
||||
},
|
||||
export const c = collection;
|
||||
|
||||
get db() {
|
||||
return client;
|
||||
},
|
||||
|
||||
c: function(name){
|
||||
return client.collection(name);
|
||||
},
|
||||
|
||||
id: function(i) {
|
||||
if(typeof i === "string"){
|
||||
return ObjectId(i);
|
||||
}
|
||||
|
||||
else if(i instanceof ObjectId) {
|
||||
return i;
|
||||
}
|
||||
|
||||
else if(i instanceof Object) {
|
||||
return "_id" in i ? ObjectId(i._id) : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
export function id(i){
|
||||
if(typeof i === 'string')return ObjectId(i);
|
||||
else if(i instanceof ObjectId) return i;
|
||||
else if(i instanceof Object) return '_id' in i ? ObjectId(i._id) : null;
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import {EventEmitter} from 'events';
|
||||
import {collection} from '../core/db';
|
||||
|
||||
export default class Data extends EventEmitter {
|
||||
constructor(storeName) {
|
||||
super();
|
||||
this.name = storeName;
|
||||
this.store = collection(storeName);
|
||||
}
|
||||
|
||||
update(document){
|
||||
this.emit('update', document);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import Data from './Data';
|
||||
|
||||
export default class Feed extends Data {
|
||||
constructor() {
|
||||
super('timeline');
|
||||
}
|
||||
|
||||
add(post) {
|
||||
let add = async(post) => {
|
||||
const id = post.id;
|
||||
const type = post.type;
|
||||
const query = {id: id, type: type};
|
||||
|
||||
await this.store.update(query, post, {upsert: true});
|
||||
|
||||
let document = await this.store.find(query).limit(1).toArray();
|
||||
document = document[0];
|
||||
// post._id = `schachlo-${Date.now()}`;
|
||||
// let document = post;
|
||||
|
||||
this.update(document);
|
||||
return document;
|
||||
};
|
||||
return add(post);
|
||||
}
|
||||
|
||||
read(id) {
|
||||
|
||||
}
|
||||
|
||||
portion() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import Feed from './Feed';
|
||||
|
||||
export default {
|
||||
feed: new Feed()
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
require('babel-core/register');
|
||||
require('babel-polyfill');
|
||||
|
||||
var config = require('./config').default;
|
||||
var db = require('./core/db');
|
||||
|
||||
db.connect(config.db.uri)
|
||||
.then(function () {
|
||||
var server = require('./server').default;
|
||||
var io = require('./io').default;
|
||||
|
||||
var app = server();
|
||||
io(app);
|
||||
app.listen(config.port);
|
||||
})
|
||||
.catch(function(e){
|
||||
console.error(e.stack);
|
||||
})
|
||||
.then(function(){
|
||||
console.log(`Start listening ${config.port}`);
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
import socketIO from 'socket.io';
|
||||
import data from './data';
|
||||
|
||||
export default function(server) {
|
||||
let io = socketIO(server);
|
||||
io.on('connection', socket => {
|
||||
console.log('a user connected');
|
||||
|
||||
socket.emit('feed', {
|
||||
type: 'test',
|
||||
id: 1,
|
||||
message: 'hello world'
|
||||
});
|
||||
|
||||
// socket.on('my other event', function (data) {
|
||||
// console.log(data);
|
||||
// });
|
||||
});
|
||||
|
||||
data.feed.on('update', i => {
|
||||
console.log(`io feed update ${JSON.stringify(i, null, '\t')}`);
|
||||
io.emit('feed', i);
|
||||
});
|
||||
}
|
||||
+1
-1
@@ -33,4 +33,4 @@ export default [
|
||||
"url": "/documents",
|
||||
"color": "yellow"
|
||||
}
|
||||
]
|
||||
];
|
||||
@@ -1,15 +0,0 @@
|
||||
import {put} from 'koa-route';
|
||||
import compose from 'koa-compose';
|
||||
import {ensureServiceAuth} from '../../middlewares/ensureServiceAuth';
|
||||
|
||||
export function add() {
|
||||
return put('/api/v1/feed', compose([
|
||||
ensureServiceAuth(),
|
||||
async ctx => {
|
||||
let post = ctx.body;
|
||||
ctx.body = {
|
||||
pong: post
|
||||
};
|
||||
}
|
||||
]));
|
||||
}
|
||||
@@ -26,7 +26,7 @@ function list() {
|
||||
ctx.type = 'application/json';
|
||||
let pattern = toArray(ctx.query.sort || '');
|
||||
|
||||
let data = await c('collective').find();
|
||||
let data = await c('collective').find().toArray();
|
||||
if (!data) {
|
||||
ctx.status = 404;
|
||||
return;
|
||||
|
||||
+4
-3
@@ -1,4 +1,5 @@
|
||||
import path from 'path';
|
||||
import {Server} from 'http';
|
||||
import Koa from 'koa';
|
||||
import serve from 'koa-static';
|
||||
import logger from 'koa-logger';
|
||||
@@ -12,13 +13,13 @@ import bodyParser from 'koa-bodyparser';
|
||||
import config from './config';
|
||||
import {redirectionTable} from './config';
|
||||
import {routes, queryObject} from './routes';
|
||||
import api from './routes/api';
|
||||
import api from './api';
|
||||
import {redirect} from './routes/redirect';
|
||||
|
||||
const dirPublic = path.join(__dirname, '../public');
|
||||
const dirTemplates = path.join(__dirname, 'templates');
|
||||
|
||||
export function server(){
|
||||
export default function(){
|
||||
const $ = convert;
|
||||
|
||||
const app = new Koa();
|
||||
@@ -38,7 +39,7 @@ export function server(){
|
||||
app.use(queryObject());
|
||||
app.use(routes());
|
||||
|
||||
return app;
|
||||
return Server(app.callback());
|
||||
}
|
||||
|
||||
function prerenderRmFragment(){
|
||||
|
||||
Reference in new issue
Block a user