-
Notifications
You must be signed in to change notification settings - Fork 22
/
app.js
205 lines (175 loc) · 5.2 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
/*
* VITacademics
* Copyright (C) 2014-2016 Aneesh Neelam <[email protected]>
* Copyright (C) 2014-2016 Karthik Balakrishnan <[email protected]>
* Copyright (C) 2014-2016 Sreeram Boyapati <[email protected]>
*
* This file is part of VITacademics.
*
* VITacademics is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VITacademics is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VITacademics. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const async = require('async');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const express = require('express');
const favicon = require('serve-favicon');
const ga = require('node-ga');
//const jackrabbit = require('jackrabbit');
const logger = require('morgan');
//const mongodb = require('express-mongo-db');
const mongoClient = require('mongodb');
const path = require('path');
const underscore = require('underscore');
const config = require(path.join(__dirname, 'config'));
let newrelic;
if (config.newRelicEnabled) {
newrelic = require('newrelic');
}
let logentries;
if (config.logentriesEnabled) {
const LogentriesClient = require('logentries-client');
logentries = new LogentriesClient({
token: config.logentriesToken
});
}
const apiRoutes = require(path.join(__dirname, 'routes', 'api'));
const apiSystemRoutes = require(path.join(__dirname, 'routes', 'system'));
const txtwebRoutes = require(path.join(__dirname, 'routes', 'txtweb'));
const webRoutes = require(path.join(__dirname, 'routes', 'web'));
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
/*
* API Legacy is now deprecated
*/
const apiRoutesLegacy = require(path.join(__dirname, 'routes', 'api-legacy'));
const app = express();
// Express Logger
app.use(logger(config.expressLogLevel));
app.set('title', 'VITacademics');
// Static and Favicon
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')));
// Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Cookie Parser
app.use(cookieParser(config.expressSecretKey, {signed: true}));
// View Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// New Relic in Template
if (config.newRelicEnabled) {
app.locals.newrelic = newrelic;
}
// Google Analytics
app.use(ga(config.googleAnalyticsToken, {
safe: true
}));
// Allow Cross-Origin Resource Sharing
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// MongoDB
const mongodbOptions = {
db: {
native_parser: true,
recordQueryStats: true,
retryMiliSeconds: 500,
numberOfRetries: 10
},
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 10000
},
auto_reconnect: true,
poolSize: 50
}
};
let mongodb;
const onConnect = function (err, db) {
if (!err) {
mongodb = db;
}
else {
console.log('Failed to connect to MongoDB');
}
}
mongoClient.connect(config.mongoDb, mongodbOptions, onConnect);
app.use(function (req, res, next) {
req.db = mongodb;
next();
});
// RabbitMQ
/*
const rabbit = jackrabbit(config.amqp_Uri);
rabbit.queues = {
main: 'Main',
mobile: 'Mobile',
share: 'Share'
};
app.use(function (req, res, next) {
req.rabbit = rabbit.default();
next();
});
*/
// Routes
app.use('/', webRoutes);
app.use('/api/txtweb', txtwebRoutes);
app.use('/api/v2/system', apiSystemRoutes);
app.use('/api/v2/vellore', apiRoutes);
app.use('/api/v2/chennai', apiRoutes);
/*
* API Legacy is now deprecated
*/
app.use('/api/vellore', apiRoutesLegacy);
app.use('/api/chennai', apiRoutesLegacy);
// Catch 404 and forward to error handler
app.use(function (req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error handlers
// Development error handler, will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
if (config.logentriesEnabled) {
logentries.log('err', {Error: err, Message: err.message});
}
res.status(err.status || 500);
res.render('error', {
message: err.message,
status: err.status,
stack: err.stack,
googleAnalyticsToken: config.googleAnalyticsToken
});
});
}
// Production error handler, no stacktrace leaked to user
app.use(function (err, req, res, next) {
if (config.logentriesEnabled) {
logentries.log('err', {Error: err, Message: err.message});
}
res.status(err.status || 500);
res.render('error', {
message: err.message,
status: err.status,
stack: '',
googleAnalyticsToken: config.googleAnalyticsToken
});
});
module.exports = app;