2022-04-05 01:37:31 +08:00
|
|
|
import { Config } from '../common/config/private/Config';
|
2021-05-16 14:15:42 +08:00
|
|
|
import * as express from 'express';
|
2022-04-05 01:37:31 +08:00
|
|
|
import { Request } from 'express';
|
2018-03-31 03:30:30 +08:00
|
|
|
import * as cookieParser from 'cookie-parser';
|
|
|
|
import * as _http from 'http';
|
2022-04-05 01:37:31 +08:00
|
|
|
import { Server as HttpServer } from 'http';
|
2022-04-26 00:09:06 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2018-11-30 22:36:42 +08:00
|
|
|
// @ts-ignore
|
2018-03-31 03:30:30 +08:00
|
|
|
import * as locale from 'locale';
|
2022-04-05 01:37:31 +08:00
|
|
|
import { ObjectManagers } from './model/ObjectManagers';
|
|
|
|
import { Logger } from './Logger';
|
|
|
|
import { LoggerRouter } from './routes/LoggerRouter';
|
|
|
|
import { DiskManager } from './model/DiskManger';
|
|
|
|
import { ConfigDiagnostics } from './model/diagnostics/ConfigDiagnostics';
|
|
|
|
import { Localizations } from './model/Localizations';
|
|
|
|
import { CookieNames } from '../common/CookieNames';
|
|
|
|
import { Router } from './routes/Router';
|
|
|
|
import { PhotoProcessing } from './model/fileprocessing/PhotoProcessing';
|
2020-01-03 05:11:59 +08:00
|
|
|
import * as _csrf from 'csurf';
|
2020-01-08 05:17:54 +08:00
|
|
|
import * as unless from 'express-unless';
|
2022-04-05 01:37:31 +08:00
|
|
|
import { Event } from '../common/event/Event';
|
|
|
|
import { QueryParams } from '../common/QueryParams';
|
|
|
|
import { ConfigClassBuilder } from 'typeconfig/node';
|
|
|
|
import { ConfigClassOptions } from 'typeconfig/src/decorators/class/IConfigClass';
|
|
|
|
import { DatabaseType } from '../common/config/private/PrivateConfig';
|
2017-07-24 04:37:29 +08:00
|
|
|
|
2022-04-26 00:09:06 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2021-04-18 21:48:35 +08:00
|
|
|
const session = require('cookie-session');
|
2016-03-12 19:53:19 +08:00
|
|
|
|
2022-04-26 00:09:06 +08:00
|
|
|
declare const process: NodeJS.Process;
|
2019-12-13 02:11:49 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
const LOG_TAG = '[server]';
|
2017-07-19 16:21:52 +08:00
|
|
|
|
2016-03-12 21:57:22 +08:00
|
|
|
export class Server {
|
2020-01-03 18:36:39 +08:00
|
|
|
public onStarted = new Event<void>();
|
2021-05-16 14:15:42 +08:00
|
|
|
private app: express.Express;
|
2018-12-28 07:48:39 +08:00
|
|
|
private server: HttpServer;
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
constructor() {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (!(process.env.NODE_ENV === 'production')) {
|
2022-04-05 01:37:31 +08:00
|
|
|
Logger.info(
|
|
|
|
LOG_TAG,
|
|
|
|
'Running in DEBUG mode, set env variable NODE_ENV=production to disable '
|
|
|
|
);
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
2019-12-10 17:44:35 +08:00
|
|
|
this.init().catch(console.error);
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
2020-01-03 18:36:39 +08:00
|
|
|
get App(): any {
|
|
|
|
return this.server;
|
|
|
|
}
|
|
|
|
|
2019-12-10 17:44:35 +08:00
|
|
|
async init(): Promise<void> {
|
2018-03-31 03:30:30 +08:00
|
|
|
Logger.info(LOG_TAG, 'running diagnostics...');
|
2017-07-14 05:39:09 +08:00
|
|
|
await ConfigDiagnostics.runDiagnostics();
|
2022-04-05 01:37:31 +08:00
|
|
|
Logger.verbose(
|
|
|
|
LOG_TAG,
|
|
|
|
'using config from ' +
|
|
|
|
(
|
|
|
|
ConfigClassBuilder.attachPrivateInterface(Config)
|
|
|
|
.__options as ConfigClassOptions
|
|
|
|
).configPath +
|
|
|
|
':'
|
|
|
|
);
|
2019-12-06 22:53:34 +08:00
|
|
|
Logger.verbose(LOG_TAG, JSON.stringify(Config, null, '\t'));
|
2017-06-11 04:32:56 +08:00
|
|
|
|
2021-05-16 14:15:42 +08:00
|
|
|
this.app = express();
|
2017-06-11 04:32:56 +08:00
|
|
|
|
2017-06-21 17:32:56 +08:00
|
|
|
LoggerRouter.route(this.app);
|
2016-03-12 19:53:19 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
this.app.set('view engine', 'ejs');
|
2017-06-04 21:25:08 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
/**
|
|
|
|
* Session above all
|
|
|
|
*/
|
2017-07-16 16:59:28 +08:00
|
|
|
|
2022-04-05 01:37:31 +08:00
|
|
|
this.app.use(
|
|
|
|
session({
|
|
|
|
name: CookieNames.session,
|
|
|
|
keys: Config.Server.sessionSecret,
|
|
|
|
})
|
|
|
|
);
|
2017-10-20 00:08:07 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
/**
|
|
|
|
* Parse parameters in POST
|
|
|
|
*/
|
|
|
|
// for parsing application/json
|
2021-05-16 14:15:42 +08:00
|
|
|
this.app.use(express.json());
|
2017-12-25 07:42:25 +08:00
|
|
|
this.app.use(cookieParser());
|
2020-01-08 05:17:54 +08:00
|
|
|
const csuf: any = _csrf();
|
|
|
|
csuf.unless = unless;
|
2022-04-05 01:37:31 +08:00
|
|
|
this.app.use(
|
|
|
|
csuf.unless((req: Request) => {
|
|
|
|
return (
|
|
|
|
Config.Client.authenticationRequired === false ||
|
|
|
|
['/api/user/login', '/api/user/logout', '/api/share/login'].indexOf(
|
|
|
|
req.originalUrl
|
|
|
|
) !== -1 ||
|
|
|
|
(Config.Client.Sharing.enabled === true &&
|
|
|
|
!!req.query[QueryParams.gallery.sharingKey_query])
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2020-01-08 05:17:54 +08:00
|
|
|
|
|
|
|
// enable token generation but do not check it
|
2022-04-05 01:37:31 +08:00
|
|
|
this.app.post(
|
|
|
|
['/api/user/login', '/api/share/login'],
|
|
|
|
_csrf({ ignoreMethods: ['POST'] })
|
|
|
|
);
|
|
|
|
this.app.get(
|
|
|
|
['/api/user/me', '/api/share/:' + QueryParams.gallery.sharingKey_params],
|
|
|
|
_csrf({ ignoreMethods: ['GET'] })
|
|
|
|
);
|
2017-06-11 04:32:56 +08:00
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
DiskManager.init();
|
2019-12-15 00:27:01 +08:00
|
|
|
PhotoProcessing.init();
|
2017-12-25 07:42:25 +08:00
|
|
|
Localizations.init();
|
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
this.app.use(locale(Config.Client.languages, 'en'));
|
2021-04-18 21:48:35 +08:00
|
|
|
if (Config.Server.Database.type !== DatabaseType.memory) {
|
2019-02-16 00:47:09 +08:00
|
|
|
await ObjectManagers.InitSQLManagers();
|
2017-07-14 05:39:09 +08:00
|
|
|
} else {
|
2019-02-16 00:47:09 +08:00
|
|
|
await ObjectManagers.InitMemoryManagers();
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
2017-07-04 16:24:20 +08:00
|
|
|
|
2019-12-10 16:36:14 +08:00
|
|
|
Router.route(this.app);
|
2017-07-04 16:24:20 +08:00
|
|
|
|
|
|
|
// 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-13 00:19:51 +08:00
|
|
|
// Listen on provided PORT, on all network interfaces.
|
2018-12-28 05:14:32 +08:00
|
|
|
this.server.listen(Config.Server.port, Config.Server.host);
|
2017-07-04 16:24:20 +08:00
|
|
|
this.server.on('error', this.onError);
|
|
|
|
this.server.on('listening', this.onListening);
|
|
|
|
|
2020-01-03 18:36:39 +08:00
|
|
|
this.onStarted.trigger();
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
|
2019-02-15 07:25:55 +08: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();
|
2022-04-05 01:37:31 +08:00
|
|
|
const bind =
|
|
|
|
typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
|
2019-02-15 07:25:55 +08:00
|
|
|
Logger.info(LOG_TAG, 'Listening on ' + bind);
|
|
|
|
};
|
2016-03-12 19:53:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|