This repository was archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
274 lines (235 loc) · 7.85 KB
/
app.js
File metadata and controls
274 lines (235 loc) · 7.85 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*!
* Copyright 2014, Digium, Inc.
* All rights reserved.
*
* This source code is licensed under The AGPL v3 License found in the
* LICENSE file in the root directory of this source tree.
*
* For all details and documentation: https://www.respoke.io
*/
'use strict';
// Some libs
var fs = require('fs');
var path = require('path');
var express = require('express');
var nodemailer = require('nodemailer');
var Respoke = require('respoke-admin');
var bunyan = require('bunyan');
// Local configuration settings.
// Make sure we have the things that we need and give some friendly messages
// if stuff is missing.
var config;
var clientConfig;
(function () {
// server configuration file.
try {
config = require('./config');
} catch (ex) {
console.error('\nYou did not set up the application config correctly at ./config.js');
console.error('Copy the example from ./config.example.js and fill in your specific values.');
console.error('For setup instructions see ./README.md\n');
throw ex;
}
// Ensure all of the config values are filled out. Otherwise the app will crash later anyways.
var errors = require('./lib/check-server-config')(config);
if (errors.length) {
console.warn('\nSome configuration options are not setup properly. The app may be unstable.');
console.warn(errors, '\n');
}
})();
config.respoke.screenshareExtensionLink = config.respoke.screenshareExtensionLink || 'https://chrome.google.com/webstore/detail/apollo-screensharing-by-r/noadkfcnaibmpopkkifjangclhfgpcoo';
// Logging
var logfilePath = config.logfilePath || __dirname + "/apollo.log";
var loggerOpts = {
name: config.name,
serializers: {
req: bunyan.stdSerializers.req,
res: bunyan.stdSerializers.res,
err: bunyan.stdSerializers.err
},
streams: [{
stream: process.stdout,
level: 'info'
}, {
type: 'file',
path: logfilePath,
level: 'info'
}]
};
var logger = require('bunyan')(loggerOpts);
var loggerMiddleware = require('express-bunyan-logger')({ logger: logger });
// if logstash rotates the files, bunyan needs to know
process.on('SIGHUP', function () {
logger.reopenFileStreams();
});
// Browser configuration file
(function () {
try {
clientConfig = require('./public/js/client-config');
} catch (ex) {
logger.warn('\nYou did not set up browser config correctly at ./public/js/client-config.js');
logger.warn('Attempting to use ./public/js/client-config.example.js instead.\n');
logger.warn('To get rid of this message, copy "client-config.example.js" to "client-config.js"\n');
logger.warn(ex, '\n');
clientConfig = require('./public/js/client-config.example.js');
}
})();
// Express middleware
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var jadeStatic = require('connect-jade-static');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var browserify = require('browserify-middleware');
var middleware = require('./lib/middleware');
// Chat style themes
var themes;
if (process.env.NODE_ENV !== 'production') {
themes = [];
(function () {
var files = fs.readdirSync(__dirname + '/public/css/themes');
files.forEach(function (f) {
var parts = f.split('.');
var ext = parts[1];
if (ext === 'less') {
themes.push(parts[0]);
}
});
})();
} else {
themes = ['light', 'dark'];
}
logger.info('loaded themes', themes);
// Email sending service
var mailTransport = nodemailer.createTransport(config.smtp);
// app utilities
var appUtilities = require('./lib/app-utilities');
// MongoDB - Mongoose ODM models
var models = require('./models')(logger);
var app = express();
// Respoke setup
var respoke = new Respoke({
'App-Secret': config.respoke.appSecret,
appId: config.respoke.appId,
baseURL: config.respoke.baseURL
});
respoke.auth.connect({ endpointId: config.systemEndpointId });
respoke.on('connect', function () {
logger.info('connected to respoke');
respoke.groups.join({ groupId: config.systemGroupId }, function (err) {
if (err) {
logger.info('failed to join system group', config.systemGroupId, err);
return;
}
logger.info('joined system group', config.systemGroupId);
});
});
respoke.on('error', function (err) {
logger.error('failed to connect to respoke', err);
});
var passport = require('./auth-strategies')(respoke, logger);
app.use(favicon(__dirname + '/public/favicon.ico'));
// Sessions in mongo
app.use(session({
secret: config.mongoSessions.secret,
store: new MongoStore(config.mongoSessions),
saveUninitialized: true,
resave: true,
cookie: {
maxAge: config.cookieMaxAge
}
}));
app.use(function (req, res, next) {
req.session.touch();
next();
});
// Request parsers
app.use(bodyParser.json({ limit: config.maxUploadSize }));
app.use(bodyParser.urlencoded({ extended: false, limit: config.maxUploadSize }));
app.use(cookieParser());
// Serving static assets
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use('/js', browserify('./public/js'));
app.use(express.static(path.join(__dirname, 'public')));
if (config.respokeLocalPath) {
app.use(express.static(config.respokeLocalPath));
}
app.use(jadeStatic({
baseDir: path.join(__dirname, '/views/partials'),
baseUrl: '/partials',
jade: {
pretty: true,
config: config,
themes: themes
}
}));
// serve the docs if not production mode
if (process.env.NODE_ENV !== 'production') {
app.use('/docs', express.static(path.join(__dirname, 'docs')));
}
// Attaching app locals and utils to request
app.use(function (req, res, next) {
// Uncomment the next two lines to allow external API.
// res.set('Access-Control-Allow-Origin', '*');
// res.set('Access-Control-Allow-Headers', 'HEAD, OPTIONS, GET, POST, DELETE, PUT, PATCH');
res.set('X-Powered-By', '100-duck-sized-horses');
res.locals = req.locals || {};
res.locals.config = config;
res.locals.clientConfig = clientConfig;
res.locals.themes = themes;
req.db = models;
req.email = mailTransport;
req.utils = appUtilities;
req.respoke = respoke;
next();
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// Logger comes after the static files
app.use(loggerMiddleware);
// Passport sessions and user population on req.user
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function (account, done) {
done(null, account._id);
});
passport.deserializeUser(function (id, done) {
models.Account.findById(id, done);
});
app.use(function (req, res, next) {
if (req.user) {
res.locals.user = req.user;
}
next();
});
// Bind application routes
app.use('/', require('./routes/home'));
app.use('/api', require('./routes/api'));
app.use('/auth', require('./routes/auth'));
// Loading Apollo plugins
var normalizedPath = path.join(__dirname, 'plugins');
var pluginVars = {
db: models,
config: config,
clientConfig: clientConfig,
email: mailTransport,
respoke: respoke
};
if (fs.existsSync(normalizedPath)) {
fs.readdirSync(normalizedPath).forEach(function (file) {
var fullPath = './plugins/' + file;
logger.info('loading plugin', fullPath);
require(fullPath)(pluginVars, app);
});
}
// Error handling routes
// after plugins in case the plugins extend the app routes
app.use(middleware.fourOhFour);
app.use(middleware.errorHandler);
module.exports = app;
var server = app.listen(config.port, function() {
logger.info(config.name + ' is listening at http://localhost:' + server.address().port + '/');
app.emit('loaded');
});