1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/server.ts

151 lines
4.1 KiB
TypeScript
Raw Normal View History

2016-03-14 18:07:08 +08:00
///<reference path="../typings/main.d.ts"/>
2016-03-12 19:53:19 +08:00
import * as _express from 'express';
2016-03-19 04:36:58 +08:00
import * as _session from 'express-session';
2016-03-20 23:54:30 +08:00
import * as _bodyParser from 'body-parser';
2016-03-12 19:53:19 +08:00
import * as _debug from 'debug';
import * as _http from 'http';
2016-03-19 04:36:58 +08:00
import {PublicRouter} from "./routes/PublicRouter";
import {UserRouter} from "./routes/UserRouter";
import {GalleryRouter} from "./routes/GalleryRouter";
2016-04-11 04:58:36 +08:00
import {AdminRouter} from "./routes/AdminRouter";
import {ErrorRouter} from "./routes/ErrorRouter";
import {SharingRouter} from "./routes/SharingRouter";
2016-04-22 19:23:44 +08:00
import {Config, DatabaseType} from "./config/Config";
import {ObjectManagerRepository} from "./model/ObjectManagerRepository";
import {MongoGalleryManager} from "./model/mongoose/MongoGalleryManager";
import {MongoUserManager} from "./model/mongoose/MongoUserManager";
import {DatabaseManager} from "./model/mongoose/DatabaseManager";
2016-03-12 19:53:19 +08:00
2016-03-12 21:57:22 +08:00
export class Server {
2016-03-12 19:53:19 +08:00
2016-03-12 21:57:22 +08:00
private debug:any;
private app:any;
private server:any;
private port:number;
2016-03-12 19:53:19 +08:00
2016-03-12 21:57:22 +08:00
constructor(){
2016-03-12 19:53:19 +08:00
2016-03-12 21:57:22 +08:00
this.debug = _debug("PiGallery2:server");
this.app = _express();
2016-03-12 19:53:19 +08:00
this.app.set('view engine', 'ejs');
2016-03-12 21:57:22 +08:00
if(process.env.DEBUG) {
var _morgan = require('morgan');
this.app.use(_morgan('dev'));
}
2016-03-12 19:53:19 +08:00
2016-03-19 04:36:58 +08:00
/**
* Session above all
*/
this.app.use(_session({
name:"pigallery2-session",
secret: 'PiGallery2 secret',
2016-03-19 04:36:58 +08:00
cookie: {
maxAge: 60000*10,
httpOnly: false
2016-03-19 04:36:58 +08:00
},
resave: true,
saveUninitialized: false
}));
2016-03-20 23:54:30 +08:00
/**
* Parse parameters in POST
*/
// for parsing application/json
this.app.use(_bodyParser.json());
2016-03-27 02:24:12 +08:00
2016-04-22 19:23:44 +08:00
if(Config.databaseType === DatabaseType.memory){
ObjectManagerRepository.MemoryMongoManagers();
}else {
if (DatabaseManager.getInstance(()=>{
console.error("MongoDB connection error. Falling back to memory Object Managers");
ObjectManagerRepository.MemoryMongoManagers();
}).isConnectionError()) {
console.error("MongoDB connection error. Falling back to memory Object Managers");
ObjectManagerRepository.MemoryMongoManagers();
} else {
ObjectManagerRepository.InitMongoManagers();
}
}
2016-03-19 04:36:58 +08:00
new PublicRouter(this.app);
2016-03-19 04:36:58 +08:00
new UserRouter(this.app);
new GalleryRouter(this.app);
new SharingRouter(this.app);
new AdminRouter(this.app);
new ErrorRouter(this.app);
2016-03-13 01:11:19 +08:00
2016-03-12 19:53:19 +08:00
2016-04-11 04:58:36 +08:00
// Get port from environment and store in Express.
this.app.set('port', Config.port);
2016-03-12 19:53:19 +08:00
2016-03-12 21:57:22 +08:00
// Create HTTP server.
this.server = _http.createServer(this.app);
2016-03-12 19:53:19 +08:00
2016-03-12 21:57:22 +08:00
//Listen on provided port, on all network interfaces.
2016-04-11 04:58:36 +08:00
this.server.listen(Config.port);
2016-03-12 21:57:22 +08:00
this.server.on('error', this.onError);
this.server.on('listening', this.onListening);
2016-03-12 19:53:19 +08:00
}
2016-04-11 04:58:36 +08:00
2016-03-12 19:53:19 +08:00
2016-03-12 21:57:22 +08:00
/**
* Event listener for HTTP server "error" event.
*/
private onError = (error) => {
if (error.syscall !== 'listen') {
2016-03-12 19:53:19 +08:00
throw error;
2016-03-12 21:57:22 +08:00
}
2016-04-11 04:58:36 +08:00
var bind = typeof Config.port === 'string'
? 'Pipe ' + Config.port
: 'Port ' + Config.port;
2016-03-12 21:57:22 +08:00
// handle specific listen error with friendly messages
2016-03-12 21:57:22 +08: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 19:53:19 +08:00
}
2016-03-12 21:57:22 +08:00
if(process.env.DEBUG) {
console.log("Running in DEBUG mode");
2016-03-12 19:53:19 +08:00
}
2016-03-12 21:57:22 +08:00
new Server();