2016-03-14 12:07:08 +02:00
|
|
|
///<reference path="../typings/main.d.ts"/>
|
2016-03-12 13:53:19 +02:00
|
|
|
|
|
|
|
import * as _express from 'express';
|
2016-03-18 22:36:58 +02:00
|
|
|
import * as _session from 'express-session';
|
2016-03-20 17:54:30 +02:00
|
|
|
import * as _bodyParser from 'body-parser';
|
2016-03-12 13:53:19 +02:00
|
|
|
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";
|
2016-03-19 18:31:42 +02:00
|
|
|
import {GalleryRouter} from "./routes/GalleryRouter";
|
|
|
|
import {AdminRouter} from "./routes/AdminRouter";
|
2016-03-26 12:19:10 +02:00
|
|
|
import {Error} from "../common/entities/Error";
|
|
|
|
import {json} from "body-parser";
|
|
|
|
import {ErrorRouter} from "./routes/ErrorRouter";
|
|
|
|
import {SharingRouter} from "./routes/SharingRouter";
|
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-03-12 15:57:22 +02:00
|
|
|
private debug:any;
|
|
|
|
private app:any;
|
|
|
|
private server:any;
|
|
|
|
private port:number;
|
2016-03-12 13:53:19 +02:00
|
|
|
|
2016-03-12 15:57:22 +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
|
|
|
|
2016-04-09 18:06:29 +02:00
|
|
|
this.app.set('view engine', 'ejs');
|
|
|
|
|
2016-03-12 15:57:22 +02:00
|
|
|
if(process.env.DEBUG) {
|
|
|
|
var _morgan = require('morgan');
|
|
|
|
this.app.use(_morgan('dev'));
|
|
|
|
}
|
2016-03-12 13:53:19 +02:00
|
|
|
|
2016-03-18 22:36:58 +02:00
|
|
|
/**
|
|
|
|
* Session above all
|
|
|
|
*/
|
|
|
|
this.app.use(_session({
|
2016-03-20 21:06:14 +02:00
|
|
|
name:"pigallery2-session",
|
|
|
|
secret: 'PiGallery2 secret',
|
2016-03-18 22:36:58 +02:00
|
|
|
cookie: {
|
2016-03-20 21:06:14 +02:00
|
|
|
maxAge: 60000,
|
|
|
|
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-04-09 18:06:29 +02:00
|
|
|
|
|
|
|
|
2016-03-26 20:24:12 +02:00
|
|
|
|
|
|
|
|
2016-03-18 22:36:58 +02:00
|
|
|
|
|
|
|
new PublicRouter(this.app);
|
2016-03-26 12:19:10 +02:00
|
|
|
|
2016-03-18 22:36:58 +02:00
|
|
|
new UserRouter(this.app);
|
2016-03-19 18:31:42 +02:00
|
|
|
new GalleryRouter(this.app);
|
2016-03-26 12:19:10 +02:00
|
|
|
new SharingRouter(this.app);
|
2016-03-19 18:31:42 +02:00
|
|
|
new AdminRouter(this.app);
|
2016-03-26 12:19:10 +02:00
|
|
|
|
|
|
|
new ErrorRouter(this.app);
|
2016-03-12 19:11:19 +02:00
|
|
|
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2016-03-12 13:53:19 +02:00
|
|
|
|
|
|
|
|
2016-03-12 15:57:22 +02:00
|
|
|
// Get port from environment and store in Express.
|
|
|
|
this.port = Server.normalizePort(process.env.PORT || '80');
|
|
|
|
this.app.set('port', this.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-03-12 15:57:22 +02:00
|
|
|
//Listen on provided port, on all network interfaces.
|
|
|
|
this.server.listen(this.port);
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Normalize a port into a number, string, or false.
|
|
|
|
*/
|
|
|
|
private static normalizePort(val) {
|
|
|
|
var port = parseInt(val, 10);
|
2016-03-12 13:53:19 +02:00
|
|
|
|
2016-03-12 15:57:22 +02:00
|
|
|
if (isNaN(port)) {
|
|
|
|
// named pipe
|
|
|
|
return val;
|
|
|
|
}
|
2016-03-12 13:53:19 +02:00
|
|
|
|
2016-03-12 15:57:22 +02:00
|
|
|
if (port >= 0) {
|
|
|
|
// port number
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-03-12 13:53:19 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 15:57:22 +02:00
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "error" event.
|
|
|
|
*/
|
|
|
|
private onError = (error) => {
|
|
|
|
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 this.port === 'string'
|
|
|
|
? 'Pipe ' + this.port
|
|
|
|
: 'Port ' + this.port;
|
|
|
|
|
2016-03-26 12:19:10 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-12 15:57:22 +02:00
|
|
|
|
|
|
|
if(process.env.DEBUG) {
|
|
|
|
console.log("Running in DEBUG mode");
|
2016-03-12 13:53:19 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 15:57:22 +02:00
|
|
|
new Server();
|