forked from soygul/koan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
44 lines (37 loc) · 1.28 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
/**
* Entry point for KOAN app. Initiates database connection and starts listening for requests on configured port.
*/
var config = require('./server/config/config'),
mongo = require('./server/config/mongo'),
mongoSeed = require('./server/config/mongo-seed'),
koaConfig = require('./server/config/koa'),
ws = require('./server/config/ws'),
co = require('co'),
koa = require('koa'),
app = koa();
module.exports = app;
/**
* Initiates a new KOAN server. Returns a promise.
* @param overwriteDB Overwrite existing database with the seed data. Useful for testing environment.
*/
app.init = co.wrap(function *(overwriteDB) {
// initialize mongodb and populate the database with seed data if empty
yield mongo.connect();
yield mongoSeed(overwriteDB);
// koa config
koaConfig(app);
// create http and websocket servers and start listening for requests
app.server = app.listen(config.app.port);
ws.listen(app.server);
if (config.app.env !== 'test') {
console.log('KOAN listening on port ' + config.app.port);
}
});
// auto init if this app is not being initialized by another module (i.e. using require('./app').init();)
if (!module.parent) {
app.init().catch(function (err) {
console.error(err.stack);
process.exit(1);
});
}