-
Notifications
You must be signed in to change notification settings - Fork 4
/
web-server-backup.js
executable file
·270 lines (224 loc) · 7.75 KB
/
web-server-backup.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
var express = require("express"),
app = express(),
port = parseInt(process.env.PORT, 10) || 8090,
program = require('commander'),
fs = require('fs'),
request = require('request'),
async = require('async'),
sprintf = require('sprintf').sprintf;
// remember cookie
var request = request.defaults({jar: true});
// read parameters from command line or from environment variables
// (CAMOMILE_API, CAMOMILE_LOGIN, CAMOMILE_PASSWORD, PYANNOTE_API)
program
.option('--camomile <url>', 'URL of Camomile server (e.g. https://camomile.fr/api)')
.option('--login <login>', 'Login for Camomile server (for queues creation)')
.option('--password <password>', 'Password for Camomile server')
.option('--pyannote <url>', 'URL of PyAnnote server (e.g. https://camomile.fr/tool)')
.parse(process.argv);
var camomile_api = program.camomile || process.env.CAMOMILE_API;
var login = program.login || process.env.CAMOMILE_LOGIN;
var password = program.password || process.env.CAMOMILE_PASSWORD;
var pyannote_api = program.pyannote || process.env.PYANNOTE_API;
// configure express app
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.static(__dirname + '/app'));
app.use(app.router);
});
// handle the hidden form submit
app.post('/', function(req, res){
console.log("là");
res.redirect('/');
});
app.get('/lig', function(req, res){
res.sendfile(__dirname + '/app/indexLIG.html');
});
app.get('/limsi', function(req, res){
res.sendfile(__dirname + '/app/indexLimsi.html');
});
// log in Camomile API and callback
function log_in(callback) {
var options = {
url: camomile_api + '/login',
method: 'POST',
body: {'username': login, 'password': password},
json: true
};
request(
options,
function (error, response, body) {
// TODO: error handling
callback(null);
});
};
// log out from Camomile API and callback
function log_out(callback) {
var options = {
url: camomile_api + '/logout',
method: 'POST'
};
request(
options,
function (error, response, body) {
// TODO: error handling
callback(null);
});
};
// delete one specific queue (based on its id) and callback
function delete_one_queue(queue, callback) {
var options = {
method: 'DELETE',
url: camomile_api + '/queue/' + queue
};
request(
options,
function (error, response, body) {
// TODO: error handling
console.log(' * deleted /queue/' + queue);
callback(error);
});
};
// delete several queues in parallel (based on their ids) and callback
function delete_queues(queues, callback) {
console.log('Deleting queues');
async.each(
queues,
delete_one_queue,
function (error) {
// TODO: error handling
callback(error);
}
);
};
// create one new queue with name `item`
// and send queue ID to the callback
function create_one_queue(item, callback) {
var options = {
method: 'POST',
body: {'name': item},
json: true,
url: camomile_api + '/queue'
};
request(
options,
function (error, response, body) {
// TODO: error handling
console.log(body);
callback(error, body._id);
});
};
// create 4 new queues in parallel (shotIn, shotOut, headIn, headOut),
// remember to delete them when process is killed,
// and send queues IDs to the next function (callback)
function create_queues(callback) {
console.log('Creating queues as user ' + login);
async.map(
['shotIn', 'shotOut', 'headIn', 'headOut'],
create_one_queue,
function(err, queues) {
// TODO: error handling
// remember to remove queues when process is sent SIGINT (Ctrl+C)
// hack to account for Win32 platforms, where SIGINT does not exist
if (process.platform === "win32") {
require("readline").createInterface({
input: process.stdin,
output: process.stdout
}).on("SIGINT", function () {
process.emit("SIGINT");
});
}
process.on('SIGINT', function() {
async.waterfall(
[log_in, function(callback) { delete_queues(queues, callback); }, log_out],
function (error) {
// TODO: error handling
process.exit();
}
);
});
queues_dict = {
'shotIn': queues[0],
'shotOut': queues[1],
'headIn': queues[2],
'headOut': queues[3]
};
callback(null, queues_dict);
}
);
};
// create NodeJS route "GET /config" returning front-end configuration as JSON
// and callback (passing no results whatsoever)
function create_config_route(queues, callback) {
// ~~~~ Sample /config response ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// {
// 'camomile_api': 'http://camomile.fr/api',
// 'pyannote_api': 'http://pyannote.lu',
// 'queues': {
// 'shotIn': '54476ba692e66a08009cc355',
// 'shotOut': '54476ba692e66a08009cc356',
// 'headIn': '54476ba692e66a08009cc357',
// 'headOut': '54476ba692e66a08009cc358',
// }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var get_config = function(req, res) {
res.json({
'camomile_api': camomile_api,
'pyannote_api': pyannote_api,
'queues': {
'shotIn': queues.shotIn,
'shotOut': queues.shotOut,
'headIn': queues.headIn,
'headOut': queues.headOut
}
});
};
app.get('/config', get_config);
console.log(' * shotIn --> /queue/' + queues.shotIn);
console.log(' * shotOut --> /queue/' + queues.shotOut);
console.log(' * headIn --> /queue/' + queues.headIn);
console.log(' * headOut --> /queue/' + queues.headOut);
callback(null);
}
// create AngularJS module 'Config' in /app/config.js ('DataRoot' + 'ToolRoot')
// and callback (passing no results whatsoever)
// WARNING: this should be deprecated in favor of route "GET /config"
function create_config_file(callback) {
// ~~~~ Sample /app/config.js ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// angular.module('myApp.config', [])
// .value('DataRoot', 'http://camomile.fr/api')
// .value('ToolRoot', 'http://pyannote.lu');
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
config_js = sprintf(
"angular.module('myApp.config', [])" + "\n" +
" .value('DataRoot', '%s')" + "\n" +
" .value('ToolRoot', '%s');",
camomile_api, pyannote_api
);
fs.writeFile(
__dirname + '/app/config.js', config_js,
function(err) {
if(err) {
console.log(err);
} else {
callback(null);
}
}
);
};
// run app when everything is set up
function run_app(err, results) {
// TODO: error handling
app.listen(port);
console.log('App is running at http://localhost:' + port + ' with');
console.log(' * Camomile API --> ' + camomile_api);
console.log(' * PyAnnote API --> ' + pyannote_api);
};
// this is where all these functions are actually called, in this order:
// log in, create queues, create route /config, log out, create /app/config.js
// and (then only) run the app
async.waterfall(
[log_in, create_queues, create_config_route, log_out, create_config_file],
run_app
);