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

135 lines
3.6 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 _debug from "debug";
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-12-27 00:36:38 +02:00
import {DatabaseType} from "../common/config/Config";
2016-04-22 13:23:44 +02:00
import {ObjectManagerRepository} from "./model/ObjectManagerRepository";
import {Config} from "./config/Config";
2016-03-12 13:53:19 +02:00
2016-03-12 15:57:22 +02:00
export class Server {
2016-03-12 13:53:19 +02:00
2016-12-27 00:36:38 +02:00
private debug: any;
private app: any;
private server: any;
2016-03-12 13:53:19 +02:00
constructor() {
2016-03-12 13:53:19 +02:00
2016-03-12 15:57:22 +02:00
this.debug = _debug("PiGallery2:server");
this.app = _express();
2016-03-12 13:53:19 +02:00
this.app.set('view engine', 'ejs');
if (process.env.DEBUG) {
2016-12-27 00:36:38 +02:00
let _morgan = require('morgan');
2016-03-12 15:57:22 +02:00
this.app.use(_morgan('dev'));
}
2016-03-12 13:53:19 +02:00
2016-05-12 11:00:46 +02:00
2016-03-18 22:36:58 +02:00
/**
* Session above all
*/
this.app.use(_session({
name: "pigallery2-session",
secret: 'PiGallery2 secret',
2016-03-18 22:36:58 +02:00
cookie: {
maxAge: 60000 * 10,
httpOnly: false
2016-03-18 22:36:58 +02:00
},
resave: true,
saveUninitialized: false
}));
2016-03-20 17:54:30 +02:00
/**
* Parse parameters in POST
*/
// for parsing application/json
this.app.use(_bodyParser.json());
2016-03-26 20:24:12 +02:00
2016-05-09 17:04:56 +02:00
2016-12-27 21:55:51 +02:00
ObjectManagerRepository.InitMySQLManagers().catch((err) => {
2016-12-27 17:09:47 +02:00
console.error("Erro during initailizing mysql falling back to memory DB");
2016-12-27 21:55:51 +02:00
console.log(err);
2016-12-27 17:09:47 +02:00
Config.setDatabaseType(DatabaseType.memory);
ObjectManagerRepository.InitMemoryManagers();
});
2016-03-18 22:36:58 +02:00
new PublicRouter(this.app);
2016-03-18 22:36:58 +02:00
new UserRouter(this.app);
new GalleryRouter(this.app);
new SharingRouter(this.app);
new AdminRouter(this.app);
2016-03-12 19:11:19 +02:00
new ErrorRouter(this.app);
2016-03-12 13:53:19 +02:00
2016-05-01 10:50:17 +02:00
// Get PORT from environment and store in Express.
this.app.set('port', Config.Server.port);
2016-03-12 13:53:19 +02:00
2016-03-12 15:57:22 +02:00
// Create HTTP server.
this.server = _http.createServer(this.app);
2016-03-12 13:53:19 +02:00
2016-05-01 10:50:17 +02:00
//Listen on provided PORT, on all network interfaces.
this.server.listen(Config.Server.port);
2016-03-12 15:57:22 +02:00
this.server.on('error', this.onError);
this.server.on('listening', this.onListening);
2016-03-12 13:53:19 +02:00
}
2016-03-12 15:57:22 +02:00
/**
* Event listener for HTTP server "error" event.
*/
2016-12-27 00:36:38 +02:00
private onError = (error: any) => {
2016-03-12 15:57:22 +02:00
if (error.syscall !== 'listen') {
2016-03-12 13:53:19 +02:00
throw error;
2016-03-12 15:57:22 +02:00
}
var bind = typeof Config.Server.port === 'string'
? 'Pipe ' + Config.Server.port
: 'Port ' + Config.Server.port;
2016-03-12 15:57:22 +02:00
// handle specific listen error with friendly messages
2016-03-12 15:57:22 +02:00
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
};
/**
* Event listener for HTTP server "listening" event.
*/
private onListening = () => {
var addr = this.server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
this.debug('Listening on ' + bind);
};
2016-03-12 13:53:19 +02:00
}
if (process.env.DEBUG) {
2016-03-12 15:57:22 +02:00
console.log("Running in DEBUG mode");
2016-03-12 13:53:19 +02:00
}
2016-03-12 15:57:22 +02:00
new Server();