/
app.js
218 lines (173 loc) · 5.6 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// Entry point into the NodeSpeed Framework.
// @author Roshan Kulkarni, Mindstix Labs
//
// Dependencies
var log4js = require('log4js');
var path = require('path');
var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var uuid = require('uuid');
var device = require("express-device");
var fsWalk = require('fs-walk');
var adaro = require('adaro');
//
// Application Root
//
global.appRoot = __dirname;
console.log("Application Root: %s", global.appRoot);
//
// Directory Paths (Framework's Convention)
//
var configPath = path.join(global.appRoot, "/application/config/");
var controllerPath = path.join(global.appRoot, "/application/controllers/");
var controllerRoutesPath = path.join(global.appRoot, "/application/routes/controllers/");
var interceptorPath = path.join(global.appRoot, "/application/interceptors/");
var interceptorRoutesPath = path.join(global.appRoot, "/application/routes/interceptors/");
var modelPath = path.join(global.appRoot, "/application/models/");
var servicePath = path.join(global.appRoot, "/application/services/");
var publicResourcesPath = path.join(global.appRoot, "/application/public/");
var viewsPath = path.join(global.appRoot, "/application/views/");
//
// Current Runtime Environment:
// Config module uses NODE_ENV variable to determine the configuration file to be loaded from the /config directory.
//
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development';
}
console.log("Runtime Environment: %s", process.env.NODE_ENV);
//
// App Configuration:
// node-config module uses the process.env.NODE_CONFIG_DIR to determine the config directory.
//
if (!process.env.NODE_CONFIG_DIR) {
process.env.NODE_CONFIG_DIR = configPath;
}
console.log("Configuration Directory: %s", process.env.NODE_CONFIG_DIR);
var config = require('config');
console.log("Config Base: " + config.util.getEnv('NODE_CONFIG_DIR'));
console.log("Config File: " + config.util.getEnv('NODE_ENV'));
//
// Logger Initialization
//
var loggerConfig = config.get("logger");
log4js.configure(loggerConfig);
//
// Framework Components
//
var dbFactory = require('./framework/DBFactory');
var serviceFactory = require('./framework/ServiceFactory');
var interceptorFactory = require('./framework/InterceptorFactory');
var controllerFactory = require('./framework/ControllerFactory');
//
// Dump Environment Information
//
var logger = log4js.getLogger('ApplicationBootstrap');
logger.info("Environment: %s", process.env.NODE_ENV);
logger.info("AppRoot: %s", global.appRoot);
logger.info("Config Base: %s", config.util.getEnv('NODE_CONFIG_DIR'));
logger.info("Current Environment: %s", config.util.getEnv('NODE_ENV'));
logger.info("Controllers: %s", controllerPath);
logger.info("Interceptors: %s", interceptorPath);
logger.info("Models: %s", modelPath);
logger.info("Services: %s", servicePath);
logger.info("Public Resources: %s", publicResourcesPath);
//
// Instantiate Express Framework
//
var app = express();
global.app = app;
//
// Middleware: Wrap all requests in a domain to be able to handle any async failures in those.
//
var errorHandler = require("./framework/middleware/ErrorHandler");
app.use(errorHandler.domainWrappingMiddleware);
//
// Middleware: Cookie Parser.
// Parses the HTTP Header Cookies and populates the 'req.cookies' having name-value pairs.
// {cookieName: cookieValue, ...}
//
app.use(cookieParser());
//
// Middleware: Body Parser.
// Parses the HTTP Body and populates the req.body.
//
app.use(bodyParser.json());
//
// Middleware: Tagging Each Request with Unique Id.
// Useful to debug or trace requests in the log files.
// Access using req.uuid in the rest of the code.
// https://www.npmjs.com/package/uuid
//
app.use(function(req, res, next) {
req.uuid = uuid.v4()
logger.info("Request UUID: %s", req.uuid);
next();
});
//
// Middleware: Identify Request Channel (User Agent: desktop, phone, tablet, tv)
// Inserts the client's device type in req.device = {"type": "desktop"}
// https://github.com/rguerreiro/express-device
//
app.use(device.capture());
//
// Middeware: Deliver Static Resources under '/public/'
// http://expressjs.com/guide/using-middleware.html#middleware.built-in
//
var options = {
dotfiles: 'deny',
etag: true,
index: false,
lastModified: true,
redirect: false
};
app.use('/public', express.static(publicResourcesPath, options));
//
// View Rendering Engine
//
app.engine('dust', adaro.dust());
app.set('views', viewsPath);
app.set('view engine', 'dust');
//
// Initialize DB Connection and Models.
// global.app.db: Is a reference to the DB Factory.
//
app.db = dbFactory;
app.db.initialize(modelPath);
//
// Instantiate All Services.
// global.app.services: Is a reference to the Service Factory.
//
app.services = serviceFactory;
app.services.initialize(servicePath);
//
// Mount All Request Interceptors.
// global.app.interceptors: Is a reference to the Interceptor Factory.
//
app.interceptors = interceptorFactory;
app.interceptors.initialize(interceptorPath, interceptorRoutesPath);
//
// Initialize All Controllers. Mount All Routes to Respective Controller Methods.
// global.app.controllers: Is a reference to the Controller Factory.
//
app.controllers = controllerFactory;
app.controllers.initialize(controllerPath, controllerRoutesPath);
//
// Health Check Endpoint
//
app.get("/sys/health/ping", function(req, res) {
res.send("Ok.");
return;
});
//
// Middleware: Catch-All error handler in express.
//
app.use(errorHandler.errorHandlingMiddleware);
//
// Bind HTTP Server
//
var port = config.get("server.port");
var server = app.listen(port, function() {
logger.info("Listening on Port: %d", port);
});