2015-07-09 23:01:43 +01:00
|
|
|
import koa from 'koa';
|
|
|
|
import mount from 'koa-mount';
|
2015-08-08 20:37:49 +01:00
|
|
|
import route from 'koa-route';
|
|
|
|
import websockify from 'koa-websocket';
|
|
|
|
import redis from 'redis-url';
|
|
|
|
import coRedis from 'co-redis';
|
|
|
|
import co from 'co';
|
2015-07-09 23:01:43 +01:00
|
|
|
import api from './api/app';
|
2015-08-08 20:37:49 +01:00
|
|
|
import { events as fileEvents } from './api/routes/file';
|
|
|
|
import { events as userEvents } from './api/routes/user';
|
2015-07-09 23:01:43 +01:00
|
|
|
import web from './web/app';
|
|
|
|
import { init as storageInit } from './lib/storage';
|
|
|
|
|
|
|
|
import debugname from 'debug';
|
|
|
|
const debug = debugname('hostr');
|
|
|
|
|
|
|
|
storageInit();
|
|
|
|
|
2015-08-08 20:37:49 +01:00
|
|
|
const app = websockify(koa());
|
2015-07-09 23:01:43 +01:00
|
|
|
|
|
|
|
app.keys = [process.env.KEYS || 'INSECURE'];
|
|
|
|
|
2015-08-08 20:37:49 +01:00
|
|
|
const redisUrl = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://localhost:6379';
|
|
|
|
|
|
|
|
let coRedisConn = {};
|
|
|
|
|
|
|
|
co(function*() {
|
|
|
|
coRedisConn = coRedis(redis.connect(redisUrl));
|
|
|
|
coRedisConn.on('error', function (err) {
|
|
|
|
debug('Redis error ' + err);
|
|
|
|
});
|
|
|
|
}).catch(function(err) {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
app.ws.use(function*(next) {
|
|
|
|
this.redis = coRedisConn;
|
|
|
|
yield next;
|
|
|
|
});
|
|
|
|
|
|
|
|
app.ws.use(route.all('/api/user', userEvents));
|
|
|
|
app.ws.use(route.all('/api/file/:id', fileEvents));
|
|
|
|
|
2015-07-09 23:01:43 +01:00
|
|
|
app.use(mount('/api', api));
|
|
|
|
app.use(mount('/', web));
|
|
|
|
|
|
|
|
if (!module.parent) {
|
2015-08-08 20:37:49 +01:00
|
|
|
app.listen(process.env.PORT || 4040, function() {
|
|
|
|
debug('Koa HTTP server listening on port ' + (process.env.PORT || 4040));
|
|
|
|
});
|
2015-08-11 21:54:55 +01:00
|
|
|
setInterval(function() {
|
|
|
|
debug('%sMB', process.memoryUsage().rss / 1024 / 1024);
|
|
|
|
}, 10000);
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
|
2015-08-11 21:54:55 +01:00
|
|
|
export default app;
|