forked from maksrom/javascript-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.js
More file actions
executable file
·102 lines (77 loc) · 2.5 KB
/
application.js
File metadata and controls
executable file
·102 lines (77 loc) · 2.5 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Custom application, inherits from Koa Application
* Gets requireModules which adds a module to handlers.
*
* Handlers are called on:
* - init (sync) - initial requires
* - boot (async) - ensure ready to get a request
* - close (async) - close connections
*
* @type {Application}
*/
const KoaApplication = require('koa');
const inherits = require('inherits');
const log = require('log')();
module.exports = Application;
function Application() {
KoaApplication.apply(this, arguments);
this.handlers = {};
this.log = log;
}
inherits(Application, KoaApplication);
// wait for full app load and all associated warm-ups to finish
// mongoose buffers queries,
// so for TEST/DEV there's no reason to wait
// for PROD, there is a reason: to check if DB is ok before taking a request
Application.prototype.waitBoot = function* () {
for (var path in this.handlers) {
var handler = this.handlers[path];
if (!handler.boot) continue;
yield* handler.boot();
}
};
// adding middlewares only possible *before* app.run
// (before server.listen)
// assigns server instance (meaning only 1 app can be run)
//
// app.listen can also be called from tests directly (and synchronously), without waitBoot (many times w/ random port)
// it's ok for tests, db requests are buffered, no need to waitBoot
Application.prototype.waitBootAndListen = function*(host, port) {
yield* this.waitBoot();
yield function(callback) {
this.server = this.listen(port, host, callback);
}.bind(this);
this.log.info('Server is listening %s:%d', host, port);
};
Application.prototype.close = function*() {
this.log.info("Closing app server...");
yield function(callback) {
this.server.close(callback);
}.bind(this);
this.log.info("App connections are closed");
for (var path in this.handlers) {
var handler = this.handlers[path];
if (!handler.close) continue;
yield* handler.close();
}
this.log.info("App stopped");
};
Application.prototype.requireHandler = function(path) {
// if debug is on => will log the middleware travel chain
if (process.env.NODE_ENV == 'development' || process.env.LOG_LEVEL) {
var log = this.log;
this.use(function *(next) {
log.trace("-> setup " + path);
var d = new Date();
yield* next;
log.trace("<- setup " + path, new Date() - d);
});
}
var handler = require(path);
// init is always sync, for tests to run fast
// boot is async
if (handler.init) {
handler.init(this);
}
this.handlers[path] = handler;
};