mdot_server/app.js

126 lines
3.1 KiB
JavaScript

/**
* Created by Martin on 08/02/2016.
*/
'use strict';
var express = require('express');
var path = require('path');
var http = require('http');
var ejs = require('ejs');
var morgan = require('morgan');
var cookieparser = require('cookie-parser');
var session = require('express-session');
var methodoverride = require('method-override');
var bodyparser = require('body-parser');
var errorhandler = require('errorhandler');
var log4js = require('log4js');
var logger = log4js.getLogger();
var authentication = require('basic-authentication');
var WebSocketServer = require('websocket').server;
var EventEmitter = require('events');
var busEmitter = new EventEmitter();
// Var apn = require('apn');
require('sugar-date');
var isProduction = false;
var mdotApi = require('./lib/mdot/api.js');
var mdotApiV2 = require('./lib/mdot/apiv2.js');
var trackApi = require('./lib/mdot/track.js');
var meetingApi = require('./lib/mdot/meeting.js');
var messageApi = require('./lib/mdot/message.js');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
if (process.env.NODE_ENV === 'production') {
isProduction = true;
}
logger.warn('isProduction:', isProduction);
var app = express();
/*App.use(authentication(({
hash: 'sha1', // type of hash
file: 'htpasswd', // path of file
suppress: true // suppress throwing Error if wrong user
})));*/
app.set('port', process.env.PORT || 4545);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(morgan('combined'));
app.use(cookieparser('your secret here'));
app.use(session({
secret: '1234567890QWERTY', resave: false, saveUninitialized: false
}));
/* 'default', 'short', 'tiny', 'dev' */
app.use(methodoverride());
app.use(bodyparser.urlencoded({extended: false}));
// Parse application/json
app.use(bodyparser.json());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
// Run npm start --production to use dist
var staticDir = isProduction ? 'dist' : 'app';
var graphFile = isProduction ? 'graph-release' : 'test';
app.use(express.static(path.join(__dirname, staticDir)));
app.use(errorhandler({dumpExceptions: true, showStack: true}));
// Events and sockets
function originIsAllowed(origin) {
// Put logic here to detect whether the specified origin is allowed.
return true;
}
app.get('/graph', authentication(({
hash: 'sha1', // Type of hash
file: 'htpasswd', // Path of file
suppress: true // Suppress throwing Error if wrong user
})), function(req, res) {
res.render(graphFile,{delimiter: '^'});
});
app.get('/meeting', function(req, res) {
res.render('meeting',{delimiter: '^'});
});
// Glue routes
mdotApi(app);
mdotApiV2(app);
trackApi(app);
meetingApi(app);
messageApi(app);
var port = (process.env.VCAP_APP_PORT || 3011);
var host = (process.env.VCAP_APP_HOST || 'localhost');
app.get('*', function(req, res) {
res.status(404).render('404',{delimiter: '^'});
});
app.listen(port, function() {
logger.info('Express listening on ',host, port);
});