1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-14 11:23:17 +02:00
pigallery2/backend/server.ts

147 lines
3.7 KiB
TypeScript
Raw Normal View History

import * as _express from "express";
import * as _session from "express-session";
import * as _bodyParser from "body-parser";
import * as _http from "http";
2016-03-18 22:36:58 +02:00
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";
2016-04-22 13:23:44 +02:00
import {ObjectManagerRepository} from "./model/ObjectManagerRepository";
import {Logger} from "./Logger";
2017-06-04 15:25:08 +02:00
import {Config} from "../common/config/private/Config";
2017-07-13 23:39:09 +02:00
import {DatabaseType} from "../common/config/private/IPrivateConfig";
2017-06-21 11:32:56 +02:00
import {LoggerRouter} from "./routes/LoggerRouter";
import {ThumbnailGeneratorMWs} from "./middlewares/thumbnail/ThumbnailGeneratorMWs";
import {DiskManager} from "./model/DiskManger";
2017-07-09 14:23:50 +02:00
import {NotificationRouter} from "./routes/NotificationRouter";
2017-07-13 23:39:09 +02:00
import {ConfigDiagnostics} from "./model/ConfigDiagnostics";
2016-03-12 13:53:19 +02:00
const LOG_TAG = "[server]";
2016-03-12 15:57:22 +02:00
export class Server {
2016-03-12 13:53:19 +02:00
private app: any;
private server: any;
constructor() {
if (process.env.DEBUG) {
Logger.debug(LOG_TAG, "Running in DEBUG mode");
}
this.init();
}
async init() {
Logger.info(LOG_TAG, "running diagnostics...");
2017-07-13 23:39:09 +02:00
await ConfigDiagnostics.runDiagnostics();
Logger.info(LOG_TAG, "using config:");
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
this.app.set('view engine', 'ejs');
2017-06-04 15:25:08 +02:00
2016-03-18 22:36:58 +02:00
/**
* Session above all
*/
this.app.use(_session({
name: "pigallery2-session",
secret: 'PiGallery2 secret',
cookie: {
maxAge: 60000 * 10,
httpOnly: false
},
resave: true,
saveUninitialized: false
}));
2017-06-04 15:25:08 +02:00
/**
* Parse parameters in POST
*/
// for parsing application/json
this.app.use(_bodyParser.json());
DiskManager.init();
ThumbnailGeneratorMWs.init();
2017-07-13 23:39:09 +02:00
if (Config.Server.database.type == DatabaseType.mysql) {
await ObjectManagerRepository.InitMySQLManagers();
} else {
await ObjectManagerRepository.InitMemoryManagers();
}
PublicRouter.route(this.app);
UserRouter.route(this.app);
GalleryRouter.route(this.app);
SharingRouter.route(this.app);
AdminRouter.route(this.app);
2017-07-09 14:23:50 +02:00
NotificationRouter.route(this.app);
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 15:57:22 +02:00
/**
* Event listener for HTTP server "error" event.
*/
private onError = (error: any) => {
if (error.syscall !== 'listen') {
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 15:57:22 +02: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 15:57:22 +02:00
2016-03-12 13:53:19 +02:00
}