data api with io
This commit is contained in:
1 parent
0ffa403b60
commit
aea2b8ff25
18 files changed
+162
-94
No files matched your search
+2
-1
@@ -33,9 +33,10 @@
|
|||||||
"koa-prerender": "^1.0.3",
|
"koa-prerender": "^1.0.3",
|
||||||
"koa-route": "^3.1.0",
|
"koa-route": "^3.1.0",
|
||||||
"koa-static": "^2.0.0",
|
"koa-static": "^2.0.0",
|
||||||
|
"mongodb": "^2.1.11",
|
||||||
"mz": "^2.3.1",
|
"mz": "^2.3.1",
|
||||||
"promised-mongo": "git+https://git@github.com/tmshv/promised-mongo",
|
|
||||||
"sitemap": "^1.5.0",
|
"sitemap": "^1.5.0",
|
||||||
|
"socket.io": "^1.4.5",
|
||||||
"thunkify": "^2.1.2"
|
"thunkify": "^2.1.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"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 Bytes from './filters/Bytes';
|
||||||
import Ext from './filters/Ext';
|
import Ext from './filters/Ext';
|
||||||
|
|
||||||
// import IOService from './services/io';
|
import IOService from './services/io';
|
||||||
import MenuService from './services/menu';
|
import MenuService from './services/menu';
|
||||||
import API from './services/api';
|
import API from './services/api';
|
||||||
|
|
||||||
@@ -225,7 +225,7 @@ app.run(($location, $rootScope, $http) => {
|
|||||||
|
|
||||||
[
|
[
|
||||||
API,
|
API,
|
||||||
// IOService,
|
IOService,
|
||||||
MenuService,
|
MenuService,
|
||||||
RemoveHashtags,
|
RemoveHashtags,
|
||||||
RemoveNewline,
|
RemoveNewline,
|
||||||
|
|||||||
@@ -27,14 +27,13 @@ function postText(post){
|
|||||||
export default function(app) {
|
export default function(app) {
|
||||||
app.component('timeline', {
|
app.component('timeline', {
|
||||||
template: template,
|
template: template,
|
||||||
controller: function($timeout, usSpinnerService, api){
|
controller: function($timeout, usSpinnerService, api, io){
|
||||||
let skip = 0;
|
let skip = 0;
|
||||||
let limit = 10;
|
let limit = 10;
|
||||||
this.feed = [];
|
this.feed = [];
|
||||||
|
|
||||||
this.timelineUpdating = false;
|
this.timelineUpdating = false;
|
||||||
usSpinnerService.stop('timelineMore');
|
usSpinnerService.stop('timelineMore');
|
||||||
//usSpinnerService.spin("timelineMore");
|
|
||||||
|
|
||||||
function toInstagram(post){
|
function toInstagram(post){
|
||||||
//var author_id = personByInstagram(team.team, post.data.author);
|
//var author_id = personByInstagram(team.team, post.data.author);
|
||||||
@@ -68,7 +67,6 @@ export default function(app) {
|
|||||||
else return post;
|
else return post;
|
||||||
});
|
});
|
||||||
|
|
||||||
//skip++;
|
|
||||||
skip += limit;
|
skip += limit;
|
||||||
this.feed = this.feed.concat(feed2);
|
this.feed = this.feed.concat(feed2);
|
||||||
this.showFooter = this.feed && this.feed.length;
|
this.showFooter = this.feed && this.feed.length;
|
||||||
@@ -77,18 +75,16 @@ export default function(app) {
|
|||||||
|
|
||||||
this.loadNext();
|
this.loadNext();
|
||||||
|
|
||||||
// io.on('post', params => {
|
io.on('feed', feed => {
|
||||||
//let posts = params instanceof Array ? params : [params];
|
// console.log(feed);
|
||||||
//
|
|
||||||
//let feed2 = posts.map(post => {
|
|
||||||
// if(post.type == 'instagram') return toInstagram(post);
|
|
||||||
// else return post;
|
|
||||||
//});
|
|
||||||
|
|
||||||
//this.$apply(() => {
|
feed = feed instanceof Array ? feed : [feed];
|
||||||
// this.feed = feed2.concat(this.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';
|
import {c} from '../core/db';
|
||||||
|
|
||||||
export async function getCollective(query={}, show={}) {
|
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) {
|
if (!data) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-41
@@ -1,46 +1,24 @@
|
|||||||
/**
|
import {MongoClient, ObjectId} from 'mongodb';
|
||||||
* Created by tmshv on 22/11/14.
|
import co from 'co';
|
||||||
*/
|
|
||||||
|
|
||||||
var mongo = require("promised-mongo");
|
export var db;
|
||||||
var ObjectId = mongo.ObjectId;
|
|
||||||
|
|
||||||
var client = null;
|
export function connect(uri){
|
||||||
|
return co(function*() {
|
||||||
|
db = yield MongoClient.connect(uri);
|
||||||
|
return db;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
export function collection(name){
|
||||||
init: function (uri) {
|
return db.collection(name);
|
||||||
//noinspection JSUnresolvedFunction
|
}
|
||||||
return new Promise(function (resolve) {
|
|
||||||
client = mongo(uri);
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
close: function () {
|
export const c = collection;
|
||||||
client.close();
|
|
||||||
},
|
|
||||||
|
|
||||||
get db() {
|
export function id(i){
|
||||||
return client;
|
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;
|
||||||
c: function(name){
|
return null;
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -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);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -6,4 +6,4 @@ export function shortifyName(name) {
|
|||||||
|
|
||||||
export function splitName(name) {
|
export function splitName(name) {
|
||||||
return /(.+)\s(.+)\s(.+)/.exec(name);
|
return /(.+)\s(.+)\s(.+)/.exec(name);
|
||||||
}
|
}
|
||||||
+1
-1
@@ -33,4 +33,4 @@ export default [
|
|||||||
"url": "/documents",
|
"url": "/documents",
|
||||||
"color": "yellow"
|
"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';
|
ctx.type = 'application/json';
|
||||||
let pattern = toArray(ctx.query.sort || '');
|
let pattern = toArray(ctx.query.sort || '');
|
||||||
|
|
||||||
let data = await c('collective').find();
|
let data = await c('collective').find().toArray();
|
||||||
if (!data) {
|
if (!data) {
|
||||||
ctx.status = 404;
|
ctx.status = 404;
|
||||||
return;
|
return;
|
||||||
|
|||||||
+5
-4
@@ -1,4 +1,5 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import {Server} from 'http';
|
||||||
import Koa from 'koa';
|
import Koa from 'koa';
|
||||||
import serve from 'koa-static';
|
import serve from 'koa-static';
|
||||||
import logger from 'koa-logger';
|
import logger from 'koa-logger';
|
||||||
@@ -12,13 +13,13 @@ import bodyParser from 'koa-bodyparser';
|
|||||||
import config from './config';
|
import config from './config';
|
||||||
import {redirectionTable} from './config';
|
import {redirectionTable} from './config';
|
||||||
import {routes, queryObject} from './routes';
|
import {routes, queryObject} from './routes';
|
||||||
import api from './routes/api';
|
import api from './api';
|
||||||
import {redirect} from './routes/redirect';
|
import {redirect} from './routes/redirect';
|
||||||
|
|
||||||
const dirPublic = path.join(__dirname, '../public');
|
const dirPublic = path.join(__dirname, '../public');
|
||||||
const dirTemplates = path.join(__dirname, 'templates');
|
const dirTemplates = path.join(__dirname, 'templates');
|
||||||
|
|
||||||
export function server(){
|
export default function(){
|
||||||
const $ = convert;
|
const $ = convert;
|
||||||
|
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
@@ -37,8 +38,8 @@ export function server(){
|
|||||||
app.use($(helmet()));
|
app.use($(helmet()));
|
||||||
app.use(queryObject());
|
app.use(queryObject());
|
||||||
app.use(routes());
|
app.use(routes());
|
||||||
|
|
||||||
return app;
|
return Server(app.callback());
|
||||||
}
|
}
|
||||||
|
|
||||||
function prerenderRmFragment(){
|
function prerenderRmFragment(){
|
||||||
|
|||||||
Reference in new issue
Block a user