-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
95 lines (76 loc) · 2.65 KB
/
index.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
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
// Copyright 2016, EMC, Inc.
'use strict';
var _ = require('lodash');
var _di = require('di');
require('./lib/extensions.js');
var self = module.exports = {
injector: null,
contextFactory: contextFactory
};
function contextFactory(di, directory) {
di = di || _di;
var helper = require('./lib/di')(di, __dirname);
return {
helper: helper,
initialize: function () {
var injector = new di.Injector(_.flattenDeep([
this.injectables
]));
this.app = injector.get('app');
this.logger = injector.get('Logger')
.initialize('Http.Server');
self.injector = injector;
return this;
},
injectables: _.flattenDeep(
[
// NPM packages
helper.simpleWrapper(_, '_'),
helper.requireWrapper('nconf'),
helper.requireWrapper('assert-plus', 'assert'),
helper.requireWrapper('fs', 'fs'),
helper.requireWrapper('path', 'path'),
helper.requireWrapper('child_process', 'child_process'),
helper.requireWrapper('bluebird', 'Promise'),
helper.requireWrapper('express', 'express'),
helper.requireWrapper('serve-index', 'serve-index'),
helper.requireWrapper('node-uuid', 'uuid'),
helper.requireWrapper('validator', 'validator'),
helper.requireWrapper('mkdirp', 'mkdirp'),
helper.requireWrapper('util', 'util'),
helper.requireWrapper('lowdb', 'lowdb'),
require('./app'),
// Glob Requirables
helper.requireGlob(__dirname + '/lib/api/*.js'),
helper.requireGlob(__dirname + '/lib/common/*.js')
]
),
};
}
if (require.main === module) { run(); }
function run() {
var context = contextFactory().initialize(),
app = context.app,
logger = context.logger;
app.start()
.then(function () {
logger.info('Server Started.');
})
.catch(function (error) {
logger.critical('Server Startup Error.', { error: error });
process.nextTick(function () {
process.exit(1);
});
});
process.on('SIGINT', function () {
app.stop()
.catch(function (error) {
logger.critical('Server Shutdown Error.', { error: error });
})
.finally(function () {
process.nextTick(function () {
process.exit(1);
});
});
});
}