-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
99 lines (75 loc) · 2.6 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var bodyParser = require('body-parser');
var express = require('express');
var http = require('http');
var socketIO = require('socket.io');
var timeout = require('connect-timeout');
var util = require('util');
var APP = module.exports.APP = require('./lib/modules');
var config = require('./config');
var httpServer = null;
var socketServer = null;
/**
* Creates a new server
*
* @api private
*/
var createHTTPServer = function() {
// Create a new Express application
var app = express();
// Setup the HTTP server
httpServer = http.createServer(app).listen(config.app.port);
// Return an error if spinning up the server failed
httpServer.once('error', function(err) {
console.log('Error while spinning up Express server');
process.exit(0);
});
// Invoke the callback when the server is spun up successful
httpServer.once('listening', function() {
console.log(util.format('Application now accepting connections at http://%s:%s', config.app.host, config.app.port));
app.use(bodyParser.urlencoded({ 'limit': '250kb', 'extended': true}));
app.use(bodyParser.json({'limit': '250kb'}));
// Make the static directories accessible for the HTTP server
app.use(express.static(__dirname + '/static'));
// Return the basic template for the request
app.get('/', function(req, res) {
return res.status(200).sendFile(__dirname + '/static/index.html');
});
// API endpoints
app.post('/api/zendesk/ticket', APP.Util.REST.ZenDesk.createZenDeskTicket);
});
};
/**
* Creates a socket server
*
* @api private
*/
var createSocketServer = function() {
// Setup a socket server
socketServer = socketIO.listen(httpServer);
// Start polling for connections
socketServer.on('connection', function(socketConnection) {
// Request the publications
socketConnection.on('PUB_GET_PUBLICATIONS', function(opts) {
APP.Search.getPublications(opts)
.then(function(publications) {
socketConnection.emit('PUB_GET_PUBLICATIONS', publications);
})
.progress(function(progress) {
socketConnection.emit('PUB_GET_PUBLICATIONS_PROGRESS', progress);
})
.fail(function(err) {
socketConnection.emit('PUB_ERROR', {'err': err});
});
});
});
};
/**
* Initiaize the application
*/
var init = function() {
// Create a HTTP server
createHTTPServer();
// Create a socket server
createSocketServer();
};
init();