2016-05-05 04:06:52 +08:00
|
|
|
import * as _express from "express";
|
|
|
|
import * as _bodyParser from "body-parser";
|
|
|
|
import * as _http from "http";
|
2016-03-19 04:36:58 +08:00
|
|
|
import {PublicRouter} from "./routes/PublicRouter";
|
|
|
|
import {UserRouter} from "./routes/UserRouter";
|
2016-03-20 00:31:42 +08:00
|
|
|
import {GalleryRouter} from "./routes/GalleryRouter";
|
2016-05-05 04:06:52 +08:00
|
|
|
import {AdminRouter} from "./routes/AdminRouter";
|
2016-03-26 18:19:10 +08:00
|
|
|
import {ErrorRouter} from "./routes/ErrorRouter";
|
|
|
|
import {SharingRouter} from "./routes/SharingRouter";
|
2016-04-22 19:23:44 +08:00
|
|
|
import {ObjectManagerRepository} from "./model/ObjectManagerRepository";
|
2017-06-04 04:35:47 +08:00
|
|
|
import {Logger} from "./Logger";
|
2017-06-04 21:25:08 +08:00
|
|
|
import {Config} from "../common/config/private/Config";
|
2017-07-14 05:39:09 +08:00
|
|
|
import {DatabaseType} from "../common/config/private/IPrivateConfig";
|
2017-06-21 17:32:56 +08:00
|
|
|
import {LoggerRouter} from "./routes/LoggerRouter";
|
2017-07-04 16:24:20 +08:00
|
|
|
import {ThumbnailGeneratorMWs} from "./middlewares/thumbnail/ThumbnailGeneratorMWs";
|
|
|
|
import {DiskManager} from "./model/DiskManger";
|
2017-07-09 20:23:50 +08:00
|
|
|
import {NotificationRouter} from "./routes/NotificationRouter";
|
2017-07-14 05:39:09 +08:00
|
|
|
import {ConfigDiagnostics} from "./model/ConfigDiagnostics";
|
2017-07-24 04:37:29 +08:00
|
|
|
|
|
|
|
const _session = require('cookie-session');
|
2016-03-12 19:53:19 +08:00
|
|
|
|
2017-06-04 04:35:47 +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 {
|
2016-03-12 19:53:19 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private app: any;
|
|
|
|
private server: any;
|
|
|
|
|
|
|
|
constructor() {
|
2017-07-17 02:49:39 +08:00
|
|
|
if (!(process.env.NODE_ENV == "production")) {
|
2017-07-04 16:24:20 +08:00
|
|
|
Logger.debug(LOG_TAG, "Running in DEBUG mode");
|
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2017-07-04 16:24:20 +08:00
|
|
|
Logger.info(LOG_TAG, "running diagnostics...");
|
2017-07-14 05:39:09 +08:00
|
|
|
await ConfigDiagnostics.runDiagnostics();
|
2017-07-04 16:24:20 +08:00
|
|
|
Logger.info(LOG_TAG, "using config:");
|
2017-06-11 04:32:56 +08:00
|
|
|
Logger.info(LOG_TAG, JSON.stringify(Config, null, '\t'));
|
|
|
|
|
|
|
|
this.app = _express();
|
|
|
|
|
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
|
|
|
|
2016-03-19 04:36:58 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
/**
|
|
|
|
* Session above all
|
|
|
|
*/
|
2017-07-16 16:59:28 +08:00
|
|
|
function s4() {
|
|
|
|
return Math.floor((1 + Math.random()) * 0x10000)
|
|
|
|
.toString(16)
|
|
|
|
.substring(1);
|
|
|
|
}
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
this.app.use(_session({
|
|
|
|
name: "pigallery2-session",
|
2017-07-16 16:59:28 +08:00
|
|
|
keys: ["key1" + s4() + s4() + s4() + s4(), "key2" + s4() + s4() + s4() + s4(), "key3" + s4() + s4() + s4() + s4()]
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2017-06-04 21:25:08 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
/**
|
|
|
|
* Parse parameters in POST
|
|
|
|
*/
|
|
|
|
// for parsing application/json
|
|
|
|
this.app.use(_bodyParser.json());
|
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
DiskManager.init();
|
|
|
|
ThumbnailGeneratorMWs.init();
|
2017-07-21 05:37:10 +08:00
|
|
|
if (Config.Server.database.type != DatabaseType.memory) {
|
|
|
|
await ObjectManagerRepository.InitSQLManagers();
|
2017-07-14 05:39:09 +08:00
|
|
|
} else {
|
|
|
|
await ObjectManagerRepository.InitMemoryManagers();
|
|
|
|
}
|
2017-07-04 16:24:20 +08:00
|
|
|
|
|
|
|
PublicRouter.route(this.app);
|
|
|
|
|
|
|
|
UserRouter.route(this.app);
|
|
|
|
GalleryRouter.route(this.app);
|
|
|
|
SharingRouter.route(this.app);
|
|
|
|
AdminRouter.route(this.app);
|
2017-07-09 20:23:50 +08:00
|
|
|
NotificationRouter.route(this.app);
|
2017-07-04 16:24:20 +08: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);
|
|
|
|
|
|
|
|
//Listen on provided PORT, on all network interfaces.
|
|
|
|
this.server.listen(Config.Server.port);
|
|
|
|
this.server.on('error', this.onError);
|
|
|
|
this.server.on('listening', this.onListening);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-12 21:57:22 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "error" event.
|
|
|
|
*/
|
|
|
|
private onError = (error: any) => {
|
|
|
|
if (error.syscall !== 'listen') {
|
2017-07-19 16:21:52 +08:00
|
|
|
Logger.error(LOG_TAG, 'Server error', error);
|
2017-06-11 04:32:56 +08:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bind = typeof Config.Server.port === 'string'
|
|
|
|
? 'Pipe ' + Config.Server.port
|
|
|
|
: 'Port ' + 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;
|
|
|
|
}
|
|
|
|
};
|
2016-03-12 21:57:22 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "listening" event.
|
|
|
|
*/
|
|
|
|
private onListening = () => {
|
|
|
|
let addr = this.server.address();
|
|
|
|
const bind = typeof addr === 'string'
|
|
|
|
? 'pipe ' + addr
|
|
|
|
: 'port ' + addr.port;
|
|
|
|
Logger.info(LOG_TAG, 'Listening on ' + bind);
|
|
|
|
};
|
2016-03-12 21:57:22 +08:00
|
|
|
|
2016-03-12 19:53:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|