-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathindex.js
150 lines (124 loc) · 4.43 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// This file kicks things off
// It initializes the api and web controllers and starts the static web service
// and sockets.
var express = require('express');
var io = require('socket.io');
var sockets;
var app = express();
var server = require('http').createServer(app);
var slash = require('express-slash');
var multipart = require('connect-multiparty');
var multiparty = multipart();
var config = require('./lib/config.js');
var modules = require('./lib/modules.js');
var middleware = config.get('middleware');
var settings = config.get('expressConfig') || [];
var load = ['lib', 'adapters', 'components', 'controllers', 'models', 'api'];
var fs = require('fs');
var clc = require('cli-color');
var value, i, z;
// Check for db config
if (fs.existsSync('./db_conf.json')) {
var dbConf = JSON.parse(fs.readFileSync('./db_conf.json'));
config.set('db', dbConf);
} else {
console.log(clc.redBright('CONFIG ERROR'));
console.log(' >> No db_conf.json file.');
return;
}
// Check for mail config
if (fs.existsSync('./mail_conf.json')) {
var mailConf = JSON.parse(fs.readFileSync('./mail_conf.json'));
config.set('mailer', mailConf);
} else {
console.log(clc.yellowBright('WARNING: ') + 'No mail_conf.json available. Email service not available.');
}
// Load all the modules
// All of the modules are then accessible via /lib/modules.js, for example:
// ---
// var modules = require('lib/modules.js');
// module.adapters.nedb
// ---
// Would give you access to the nedb adapter
for (i = 0, z = load.length; i < z; i++) {
modules.load(load[i]);
}
// Set CORS policies
// Pulls settings from /config.js
app.use(modules.lib.cors);
// Basic express config
app.enable('strict routing');
if (process.env.NODE_ENV !== 'production') {
app.use(express.logger(config.get('expressLogging')));
}
app.use(express.cookieParser());
app.use(express.cookieSession({
secret: config.get('secret'),
cookie: {
maxAge: 1209600000
}
}));
app.use(express.json());
app.use(express.urlencoded());
app.use(app.router);
app.use(slash());
// Serve static assets
app.use(express.static(config.get('env.publicHTTP') || __dirname + config.get('env.publicHTTP')));
// Set custom Express config
if (settings.length) {
modules.lib.stdout('title', 'LOADING EXPRESS SETTINGS');
for (i = 0, z = settings.length; i < z; i++) {
if (Array.isArray(settings[i]) && settings[i].length) {
value = settings[i][1] || null;
app.set(settings[i][0], value);
modules.lib.stdout('output', 'EXPRESS SETTING Applied: ' + settings[i][0] + '=' + value);
}
}
}
// Initialize custom middleware
// These can be set in the /config.js file 'middleware' property by assigning
// the corresponding /components/{name}.js, {name} as an array member
if (middleware.length) {
modules.lib.stdout('title', 'LOADING MIDDLEWARE');
for (i = 0, z = middleware.length; i < z; i++) {
if (modules.components.hasOwnProperty(middleware[i])) {
// All is good, apply the component
app.use(modules.components[middleware[i]]);
modules.lib.stdout('output', 'MIDDLEWARE Applied: ' + middleware[i]);
} else {
// No component available
modules.lib.stdout('error', 'ADAPTER Missing: ' + middleware[i]);
}
}
}
// Process API calls
// Calls the appropriate /api/{file}.js on HTTP req, ensures that controller is
// in place and properly specified and calls appropriate controller method
app.all('/api/:endpoint/*', multiparty, modules.lib.api.process);
// Listen on sockets
// Simply starts Socket.io over the server
modules.lib.stdout('title', 'STARTING SOCKETS');
sockets = io.listen(server, { log: false });
modules.lib.socketio.setIO(sockets);
modules.lib.stdout('output', 'Sockets Running');
// Initialize controllers
// Loads up each of the controllers, binds them to their specified data sources
// and runs any initialization methods
modules.lib.controllers();
// Listen on app
// Starts the app service over config'd port
server.listen(config.get('env.port'));
modules.lib.stdout('title', 'SERVER RUNNING');
modules.lib.stdout('output', 'PORT: ' + config.get('env.port'));
// If not prod, start a new console section for log output
if (process.env.NODE_ENV !== 'production') {
modules.lib.stdout('title', 'LOGGING OUTPUT');
}
// If running dev, start watch
if (process.env.NODE_ENV !== 'production') {
var cp = require('child_process');
var grunt = cp.spawn('grunt', ['--force', 'watch']);
grunt.stdout.on('data', function(data) {
console.log('%s', data);
});
}