2018-03-30 21:30:30 +02:00
|
|
|
import * as _express from 'express';
|
|
|
|
import * as _bodyParser from 'body-parser';
|
|
|
|
import * as cookieParser from 'cookie-parser';
|
|
|
|
import * as _http from 'http';
|
2018-12-28 01:48:39 +02:00
|
|
|
import {Server as HttpServer} from 'http';
|
2018-11-30 16:36:42 +02:00
|
|
|
// @ts-ignore
|
2018-03-30 21:30:30 +02:00
|
|
|
import * as locale from 'locale';
|
|
|
|
import {PublicRouter} from './routes/PublicRouter';
|
|
|
|
import {UserRouter} from './routes/UserRouter';
|
|
|
|
import {GalleryRouter} from './routes/GalleryRouter';
|
|
|
|
import {AdminRouter} from './routes/AdminRouter';
|
|
|
|
import {ErrorRouter} from './routes/ErrorRouter';
|
|
|
|
import {SharingRouter} from './routes/SharingRouter';
|
2019-02-15 18:47:09 +02:00
|
|
|
import {ObjectManagers} from './model/ObjectManagers';
|
2018-03-30 21:30:30 +02:00
|
|
|
import {Logger} from './Logger';
|
|
|
|
import {Config} from '../common/config/private/Config';
|
|
|
|
import {DatabaseType} from '../common/config/private/IPrivateConfig';
|
|
|
|
import {LoggerRouter} from './routes/LoggerRouter';
|
|
|
|
import {ThumbnailGeneratorMWs} from './middlewares/thumbnail/ThumbnailGeneratorMWs';
|
|
|
|
import {DiskManager} from './model/DiskManger';
|
|
|
|
import {NotificationRouter} from './routes/NotificationRouter';
|
2018-11-18 21:26:29 +02:00
|
|
|
import {ConfigDiagnostics} from './model/diagnostics/ConfigDiagnostics';
|
2018-03-30 21:30:30 +02:00
|
|
|
import {Localizations} from './model/Localizations';
|
|
|
|
import {CookieNames} from '../common/CookieNames';
|
2019-02-15 01:25:55 +02:00
|
|
|
import {PersonRouter} from './routes/PersonRouter';
|
2017-07-23 22:37:29 +02:00
|
|
|
|
|
|
|
const _session = require('cookie-session');
|
2016-03-12 13:53:19 +02:00
|
|
|
|
2018-03-30 21:30:30 +02:00
|
|
|
const LOG_TAG = '[server]';
|
2017-07-19 10:21:52 +02:00
|
|
|
|
2016-03-12 15:57:22 +02:00
|
|
|
export class Server {
|
2016-03-12 13:53:19 +02:00
|
|
|
|
2018-05-25 03:29:05 +02:00
|
|
|
private app: _express.Express;
|
2018-12-28 01:48:39 +02:00
|
|
|
private server: HttpServer;
|
2017-06-10 22:32:56 +02:00
|
|
|
|
|
|
|
constructor() {
|
2018-05-12 18:19:51 +02:00
|
|
|
if (!(process.env.NODE_ENV === 'production')) {
|
2018-11-30 16:36:42 +02:00
|
|
|
Logger.debug(LOG_TAG, 'Running in DEBUG mode, set env variable NODE_ENV=production to disable ');
|
2017-07-04 10:24:20 +02:00
|
|
|
}
|
2017-06-10 22:32:56 +02:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2018-03-30 21:30:30 +02:00
|
|
|
Logger.info(LOG_TAG, 'running diagnostics...');
|
2017-07-13 23:39:09 +02:00
|
|
|
await ConfigDiagnostics.runDiagnostics();
|
2018-03-30 21:30:30 +02:00
|
|
|
Logger.info(LOG_TAG, 'using config:');
|
2017-06-10 22:32:56 +02:00
|
|
|
Logger.info(LOG_TAG, JSON.stringify(Config, null, '\t'));
|
|
|
|
|
|
|
|
this.app = _express();
|
|
|
|
|
2017-06-21 11:32:56 +02:00
|
|
|
LoggerRouter.route(this.app);
|
2016-03-12 13:53:19 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
this.app.set('view engine', 'ejs');
|
2017-06-04 15:25:08 +02:00
|
|
|
|
2016-03-18 22:36:58 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
/**
|
|
|
|
* Session above all
|
|
|
|
*/
|
2017-07-16 10:59:28 +02:00
|
|
|
function s4() {
|
|
|
|
return Math.floor((1 + Math.random()) * 0x10000)
|
|
|
|
.toString(16)
|
|
|
|
.substring(1);
|
|
|
|
}
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
this.app.use(_session({
|
2017-12-25 01:42:25 +02:00
|
|
|
name: CookieNames.session,
|
2018-03-30 21:30:30 +02:00
|
|
|
keys: ['key1' + s4() + s4() + s4() + s4(), 'key2' + s4() + s4() + s4() + s4(), 'key3' + s4() + s4() + s4() + s4()]
|
2017-06-10 22:32:56 +02:00
|
|
|
}));
|
2017-06-04 15:25:08 +02:00
|
|
|
|
2017-10-19 18:08:07 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
/**
|
|
|
|
* Parse parameters in POST
|
|
|
|
*/
|
|
|
|
// for parsing application/json
|
|
|
|
this.app.use(_bodyParser.json());
|
2017-12-25 01:42:25 +02:00
|
|
|
this.app.use(cookieParser());
|
2017-06-10 22:32:56 +02:00
|
|
|
|
2017-07-04 10:24:20 +02:00
|
|
|
DiskManager.init();
|
|
|
|
ThumbnailGeneratorMWs.init();
|
2017-12-25 01:42:25 +02:00
|
|
|
Localizations.init();
|
|
|
|
|
2018-03-30 21:30:30 +02:00
|
|
|
this.app.use(locale(Config.Client.languages, 'en'));
|
2018-05-12 18:19:51 +02:00
|
|
|
if (Config.Server.database.type !== DatabaseType.memory) {
|
2019-02-15 18:47:09 +02:00
|
|
|
await ObjectManagers.InitSQLManagers();
|
2017-07-13 23:39:09 +02:00
|
|
|
} else {
|
2019-02-15 18:47:09 +02:00
|
|
|
await ObjectManagers.InitMemoryManagers();
|
2017-07-13 23:39:09 +02:00
|
|
|
}
|
2017-07-04 10:24:20 +02:00
|
|
|
|
|
|
|
PublicRouter.route(this.app);
|
|
|
|
|
|
|
|
UserRouter.route(this.app);
|
|
|
|
GalleryRouter.route(this.app);
|
2019-02-15 01:25:55 +02:00
|
|
|
PersonRouter.route(this.app);
|
2017-07-04 10:24:20 +02:00
|
|
|
SharingRouter.route(this.app);
|
|
|
|
AdminRouter.route(this.app);
|
2017-07-09 14:23:50 +02:00
|
|
|
NotificationRouter.route(this.app);
|
2017-07-04 10:24:20 +02:00
|
|
|
|
|
|
|
ErrorRouter.route(this.app);
|
|
|
|
|
|
|
|
|
|
|
|
// Get PORT from environment and store in Express.
|
|
|
|
this.app.set('port', Config.Server.port);
|
|
|
|
|
|
|
|
// Create HTTP server.
|
|
|
|
this.server = _http.createServer(this.app);
|
|
|
|
|
2018-05-12 18:19:51 +02:00
|
|
|
// Listen on provided PORT, on all network interfaces.
|
2018-12-27 23:14:32 +02:00
|
|
|
this.server.listen(Config.Server.port, Config.Server.host);
|
2017-07-04 10:24:20 +02:00
|
|
|
this.server.on('error', this.onError);
|
|
|
|
this.server.on('listening', this.onListening);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:25:55 +02:00
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "error" event.
|
|
|
|
*/
|
|
|
|
private onError = (error: any) => {
|
|
|
|
if (error.syscall !== 'listen') {
|
|
|
|
Logger.error(LOG_TAG, 'Server error', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bind = Config.Server.host + ':' + Config.Server.port;
|
|
|
|
|
|
|
|
// handle specific listen error with friendly messages
|
|
|
|
switch (error.code) {
|
|
|
|
case 'EACCES':
|
|
|
|
Logger.error(LOG_TAG, bind + ' requires elevated privileges');
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
case 'EADDRINUSE':
|
|
|
|
Logger.error(LOG_TAG, bind + ' is already in use');
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "listening" event.
|
|
|
|
*/
|
|
|
|
private onListening = () => {
|
|
|
|
const addr = this.server.address();
|
|
|
|
const bind = typeof addr === 'string'
|
|
|
|
? 'pipe ' + addr
|
|
|
|
: 'port ' + addr.port;
|
|
|
|
Logger.info(LOG_TAG, 'Listening on ' + bind);
|
|
|
|
};
|
|
|
|
|
2016-03-12 13:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-04 10:24:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|