hostr/app.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

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';
2015-08-09 13:46:09 +01:00
2015-07-09 23:01:43 +01:00
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-07-09 23:01:43 +01:00
}
module.exports = app;