forked from PastVu/pastvu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·326 lines (264 loc) · 12.4 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import ms from 'ms';
import http from 'http';
import path from 'path';
import moment from 'moment';
import makeDir from 'make-dir';
import log4js from 'log4js';
import config from './config';
import express from 'express';
import socketIO from 'socket.io';
import Utils from './commons/Utils';
import connectDb, { waitDb } from './controllers/connection';
import * as session from './controllers/_session';
import CoreServer from './controllers/serviceConnector';
import { handleSocketConnection, registerSocketRequestHendler } from './app/request';
import exitHook from 'async-exit-hook';
import { photosReady } from './controllers/photo';
import { ready as mailReady } from './controllers/mail';
import { ready as authReady } from './controllers/auth';
import { ready as regionReady, scheduleRegionStatQueueDrain } from './controllers/region';
import { ready as subscrReady } from './controllers/subscr';
import { ready as settingsReady } from './controllers/settings';
import * as routes from './controllers/routes';
import * as ourMiddlewares from './controllers/middleware';
import './models/_initValues';
import './controllers/systemjs';
export async function configure(startStamp) {
const {
env,
logPath,
storePath,
manualGarbageCollect,
listen: { hostname, port },
} = config;
makeDir.sync(path.join(storePath, 'incoming'));
makeDir.sync(path.join(storePath, 'private'));
makeDir.sync(path.join(storePath, 'protected/photos'));
makeDir.sync(path.join(storePath, 'public/avatars'));
makeDir.sync(path.join(storePath, 'public/photos'));
makeDir.sync(path.join(storePath, 'publicCovered/photos'));
const logger = log4js.getLogger('app');
const logger404 = log4js.getLogger('404.js');
logger.info('Application Hash: ' + config.hash);
await connectDb({
redis: config.redis,
mongo: { uri: config.mongo.connection, poolSize: config.mongo.pool },
logger,
});
const status404Text = http.STATUS_CODES[404];
const static404 = ({ url, method, headers: { useragent, referer } = {} }, res) => {
if (env !== 'development') {
logger404.error(JSON.stringify({ url, method, useragent, referer }));
}
res.statusCode = 404;
res.end(status404Text); // Finish with 'end' instead of 'send', that there is no additional operations (etag)
};
moment.locale(config.lang); // Set global language for momentjs
const app = express();
app.disable('x-powered-by'); // Disable default X-Powered-By
app.set('query parser', 'extended'); // Parse query with 'qs' module
app.set('views', 'views');
app.set('view engine', 'pug');
// If we need user ip through req.ips(), it will return array from X-Forwarded-For with specified length.
// https://github.com/visionmedia/express/blob/master/History.md#430--2014-05-21
app.set('trust proxy', true);
// Etag ('weak' by default), so browser will be able to specify it for request.
// Thus if browser is allowed to cache with Cache-Control header, it'll send etag in request header,
// and if generated response have same etag, server will return 304 without content (browser will get it from cache)
app.set('etag', 'weak');
// Enable chache of temlates in production
// It reduce rendering time (and correspondingly 'waiting' time of client request) dramatically
if (env === 'development') {
app.disable('view cache'); // In dev disable this, so we able to edit pug templates without server reload
} else {
app.enable('view cache');
}
// Set an object which properties will be available from all pug-templates as global variables
Object.assign(app.locals, {
pretty: false, // Adds whitespace to the resulting html to make it easier for a human to read
compileDebug: false, // Include the function source in the compiled template for better error messages
debug: false, // If set to true, the tokens and function body is logged to stdoutl (in development).
config,
});
// Alias for photos with cid from root. /5 -> /p/5
app.get(/^\/(\d{1,7})$/, (req, res) => {
res.redirect(303, '/p/' + req.params[0]);
});
app.use(ourMiddlewares.responseHeaderHook());
if (config.gzip) {
app.use(require('compression')());
}
if (config.servePublic) {
const pub = path.resolve('./public');
if (env === 'development') {
const lessMiddleware = require('less-middleware');
app.use('/style', lessMiddleware(path.join(pub, 'style'), {
force: true,
once: false,
debug: false,
render: {
compress: false,
yuicompress: false,
// sourceMap: { sourceMapFileInline: true }
},
}));
}
// Favicon need to be placed before static, because it will written from disc once and will be cached
// It would be served even on next step (at static), but in this case it would be written from disc on every req
app.use(require('serve-favicon')(
path.join(pub, 'favicon.ico'), { maxAge: ms(env === 'development' ? '1s' : '2d') })
);
app.use(express.static(pub, { maxAge: ms(env === 'development' ? '1s' : '2d'), etag: false }));
// Seal static paths, ie request that achieve this handler will receive 404
app.get(/^\/(?:img|js|style)(?:\/.*)$/, static404);
}
if (config.serveStore) {
const request = require('request');
const rewrite = require('express-urlrewrite');
const proxy = require('http-proxy-middleware');
const uploadServer = `http://${config.uploader.hostname || 'localhost'}:${config.uploader.port}`;
const downloadServer = `http://${config.downloader.hostname || 'localhost'}:${config.downloader.port}`;
// Serve files for public photos
app.use('/_p/', ourMiddlewares.serveImages(path.join(storePath, 'public/photos/'), { maxAge: ms('7d') }));
app.use(rewrite('/_p/*', '/_pr/$1')); // If public doesn't exist, try to find protected version
// Serve protected files for not public photos
const prServeMiddleware = ourMiddlewares.serveImages(path.join(storePath, 'protected/photos/'), { maxAge: ms('7d') });
app.use('/_pr/',
(req, res, next) => {
request
.get({
url: `${downloadServer}${req.originalUrl}`,
headers: req.headers,
followRedirect: false,
timeout: 1500,
})
.on('response', response => {
if (response.statusCode === 303) { // 303 means ok, user can get protected file
return prServeMiddleware(req, res, next);
}
next();
})
.on('error', err => {
logger.warn('Downloader server request error:', err.message);
next();
});
}
);
app.use(rewrite('/_pr/*', '/_prn/$1')); // If protected unavalible for user or file doesn't exist, move to covered
// Serve covered files for not public photos
app.use('/_prn/', ourMiddlewares.serveImages(path.join(storePath, 'publicCovered/photos/'), { maxAge: ms('7d') }));
// Serve avatars
app.use('/_a/', ourMiddlewares.serveImages(path.join(storePath, 'public/avatars/'), { maxAge: ms('2d') }));
// Replace unfound avatars with default one
app.get('/_a/d/*', (req, res) => {
res.redirect(302, '/img/caps/avatar.png');
});
app.get('/_a/h/*', (req, res) => {
res.redirect(302, '/img/caps/avatarth.png');
});
app.use(['/upload', '/uploadava'], proxy({ target: uploadServer, logLevel: 'warn' }));
app.use('/download', proxy({ target: downloadServer, logLevel: 'warn' }));
// Seal store paths, ie request that achieve this handler will receive 404
app.get(/^\/(?:_a|_prn)(?:\/.*)$/, static404);
}
await Promise.all([authReady, settingsReady, regionReady, subscrReady, mailReady, photosReady]);
scheduleRegionStatQueueDrain();
const httpServer = http.createServer(app);
const io = socketIO(httpServer, {
wsEngine: 'ws',
transports: ['websocket', 'polling'],
path: '/socket.io',
serveClient: false,
});
// Set zero for unlimited listeners
// http://nodejs.org/docs/latest/api/events.html#events_emitter_setmaxlisteners_n
httpServer.setMaxListeners(0);
io.sockets.setMaxListeners(0);
process.setMaxListeners(0);
io.use(handleSocketConnection); // Handler for esteblishing websocket connection
registerSocketRequestHendler(io); // Register router for socket.io events
if (env === 'development') {
require('./controllers/tpl').loadController(app);
}
if (config.serveLog) {
app.use(
'/nodelog',
require('basic-auth-connect')(config.serveLogAuth.user, config.serveLogAuth.pass),
require('serve-index')(logPath, { icons: true }),
express.static(logPath, { maxAge: 0, etag: false })
);
}
// Handle appliaction routes
routes.bindRoutes(app);
// Handle route (express) errors
routes.bindErrorHandler(app);
const manualGC = manualGarbageCollect && global.gc;
if (manualGC) {
// Call the garbage collector after a certain time
logger.info(`Manual garbage collection every ${manualGarbageCollect / 1000}s`);
} else {
logger.info('Automatic garbage collection');
}
const scheduleMemInfo = (function () {
const INTERVAL = manualGC ? manualGarbageCollect : ms('30s');
function memInfo() {
let memory = process.memoryUsage();
let elapsedMs = Date.now() - startStamp;
let elapsedDays = Math.floor(elapsedMs / Utils.times.msDay);
if (elapsedDays) {
elapsedMs -= elapsedDays * Utils.times.msDay;
}
logger.info(
`+${elapsedDays}.${Utils.hh_mm_ss(elapsedMs, true)} `,
`rss: ${Utils.format.fileSize(memory.rss)}`,
`heapUsed: ${Utils.format.fileSize(memory.heapUsed)},`,
`heapTotal: ${Utils.format.fileSize(memory.heapTotal)}`,
manualGC ? '-> Starting GC' : ''
);
if (manualGC) {
const start = Date.now();
global.gc(); // Call garbage collector
memory = process.memoryUsage();
elapsedMs = Date.now() - startStamp;
elapsedDays = Math.floor(elapsedMs / Utils.times.msDay);
logger.info(
`+${elapsedDays}.${Utils.hh_mm_ss(elapsedMs, true)} `,
`rss: ${Utils.format.fileSize(memory.rss)}`,
`heapUsed: ${Utils.format.fileSize(memory.heapUsed)},`,
`heapTotal: ${Utils.format.fileSize(memory.heapTotal)}`,
`Garbage collected in ${(Date.now() - start) / 1000}s`
);
}
scheduleMemInfo();
}
return function (delta = 0) {
setTimeout(memInfo, INTERVAL + delta);
};
}());
logger.info(`Socket.io engine: ${io.engine.wsEngine}`);
logger.info(`servePublic: ${config.servePublic}, serveStore ${config.serveStore}`);
logger.info(`Host for users: [${config.client.host}]`);
await new CoreServer('Core', { port: config.core.port, host: '0.0.0.0' }, logger).listen();
httpServer.listen(port, hostname, () => {
logger.info(
`HTTP server started up in ${(Date.now() - startStamp) / 1000}s`,
`and listening [${hostname || '*'}:${port}]`,
config.gzip ? 'with gzip' : '',
'\n'
);
scheduleMemInfo(startStamp - Date.now());
});
exitHook(cb => {
logger.info('HTTP server is shutting down');
httpServer.close(cb);
});
// Once db is connected, start some periodic jobs.
// Do it in app.js, not in controllers, to prevent running these jobs on other instances (sitemap, uploader, downloader etc.)
waitDb.then(() => {
session.checkSessWaitingConnect();
if (config.primary) {
session.checkExpiredSessions();
session.calcUserStatsJob();
}
});
}