This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
executable file
·171 lines (141 loc) · 4.5 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
/**
* @file
* First attempt on creating a proxy with socket.io.
*/
// Setup the basic variables need to create the server
var path = require('path');
var express = require('express');
var fs = require('fs');
// Parse commandline args.
var argv = require('minimist')(process.argv.slice(2));
// Start the app.
var app = express();
// Load configuration.
var config = require('./lib/configuration');
// Add logger.
var Log = require('log')
var logger = new Log('info', fs.createWriteStream(config.get('log'), {'flags': 'a'}));
// As the server proxies (nginx) the web-socket and socket.io.js
// request. This trick is used to make the local instances work.
var http = undefined;
var server = undefined;
if (config.get('ssl').active) {
// Certificate settings.
var options = {
key: fs.readFileSync(config.get('ssl').key),
cert: fs.readFileSync(config.get('ssl').cert),
//ca: fs.readFileSync('./etc/ssl/ebscerts/bundle.crt')
};
if (config.get('ssl').key.ca) {
options.ca = fs.readFileSync(config.get('ssl').key.ca);
}
// Start the https server.
http = require('https');
server = http.createServer(options, app);
}
else {
// Start the http server.
http = require('http');
server = http.createServer(app);
}
// Add socket.io to the mix.
var connection = require('./lib/connection');
connection.connect(server, config.get('debug'), config.get('secret'));
// Set express app configuration.
app.set('port', config.get('port'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
app.use(express.favicon());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
// Log express requests.
if (config.get('debug')) {
app.use(express.logger('dev'));
};
// Start the server.
server.listen(app.get('port'), function (){
if (config.get('debug')) {
console.log('Express server with socket.io is listening on port ' + app.get('port'));
}
});
/************************************
* Load application objects
**************************/
var screens = require('./lib/screens');
/************************************
* Socket events
*
* @todo: This may be able to be moved inside the objects.
***************/
connection.on('connection', function(client) {
/**
* Ready event.
*/
client.on('ready', function (data) {
// Create new screen object. @todo move into screens.
var instance = screens.createScreen(data.token, client);
instance.load();
// Actions when screen have been loaded.
instance.on('loaded', function (data) {
// Join rooms/groups.
var groups = instance.get('groups');
client.join(groups);
// Send a 200 ready code back to the client.
client.ready(200);
// Push channels to the screen, if any channels exists.
instance.push();
});
// Handle errors.
instance.on('error', function (data) {
// All errors are automatically logged in Base class.
if (data.code === 404) {
// If screen is not known any more dis-connect.
client.kick(data.code);
}
});
});
// If client disconnects remove the screen from the active list.
client.on('disconnect', function(data) {
screens.removeScreen(client.getToken());
});
/**
* Pause event.
*/
client.on('pause', function (data) {
// Get client groups.
var instance = screens.get(client.getToken());
var groups = instance.get('groups');
// Remove the client from the rooms/groups.
client.leave(groups);
// Send feedback to the client.
client.pause(200);
});
});
/************************************
* Application routes
********************/
var routes = require('./routes/local');
app.get('/', routes.index);
app.post('/login', function(req, res) {
routes.login(req, res, config.get('secret'));
});
/************************************
* Backend API
*************/
var routes_backend = require('./routes/backend');
app.post('/screen/update', routes_backend.screenUpdate);
app.post('/screen/reload', routes_backend.screenReload);
app.post('/screen/remove', routes_backend.screenRemove);
app.post('/push/channel', routes_backend.pushChannel);
app.post('/push/emergency', routes_backend.pushEmergency);
app.post('/status', routes_backend.status);
app.get('/status/all', routes_backend.statusAll);
/************************************
* Client API
************/
var routes_frontend = require('./routes/frontend');
app.post('/activate', function (req, res) {
routes_frontend.activate(req, res, config.get('secret'));
});