-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTP_lib.js
executable file
·234 lines (208 loc) · 7.9 KB
/
HTTP_lib.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
const express = require('express')
const app = express();
const https = require('https');
const http = require('http')
var {spawn} = require('child_process');
var events = require ('events');
const fs = require('fs');
var bodyParser = require('body-parser');
cert_file = "/certs/detbelt-dev_ibcp_fr_full.pem"
cert_key = "/certs/detbelt-dev_ibcp_fr.key"
const server = https.createServer({
key: fs.readFileSync(cert_key),
cert: fs.readFileSync(cert_file),
}, app);
const io = require('socket.io')(server);
//var port = 3001;
/*
* Parsing a JSON file thanks to a @path [string] and returns a JSON [object]
*/
var parseConfig = function (path){
if (! path) console.log('ERROR in parseConfig : no path specified');
try { var obj = jsonfile.readFileSync(path); }
catch (err) { throw err; }
return obj;
};
/*
* Read a file thanks to a @path [string] and return its @content [string]
*/
var readFileContent = function (path){
if (! path) console.log('ERROR in readFileContent : no path specified');
try { var content = fs.readFileSync(path, 'utf8'); }
catch (err) { throw err; }
return content;
};
/*
* For back-end tests
* @worker [function] : to run
* @pdbFile [string] : path to the PDB of the user
* @deterFile [string] : path to the detergent file of the user (contains the quantities of each detergent)
* @requestPPM [boolean] : define if we need to run PPM (true) or not (false)
*/
var backCompute = function (worker, pdbFile, deterFile, requestPPM) {
if (! worker) throw 'No worker function detected';
if (! pdbFile) throw 'No PDB file detected';
if (! deterFile) throw 'No detergent file detected';
var data_input = {
'fileContent' : readFileContent(pdbFile),
'deterData' : readFileContent(deterFile),
'requestPPM' : requestPPM
};
worker(data_input)
.on('jobCompletion', function (jsonRes, jobObject) {
console.log('end of back-end tests : ');
console.log(jsonRes);
});
}
/*
* Define routes for the client
*/
var setClientRoute = function(app, downloadRoute) {
app.use(bodyParser.json({limit : '5mb'})); // for parsing application/json
app.use(bodyParser.urlencoded({limit : '5mb', extended : false}));
app.use('/bundle', express.static(__dirname + '/dist'));
app.use('/assets', express.static(__dirname + '/src/assets'));
app.use('/fonts', express.static(__dirname + '/src/assets/fonts'));
app.use('/img', express.static(__dirname + '/src/assets/img'));
app.use('/ngl', express.static(__dirname + '/src/app/ngl-master/dist'));
app.use('/resultSample', express.static(__dirname + '/data/resultSample/'));
app.use('/download', express.static(downloadRoute));
app.get('/tutorial', function(req, res) {
res.sendFile(__dirname + '/src/tutorial.html');
});
app.get('/gallery', function(req, res) {
res.sendFile(__dirname + '/src/gallery.html');
});
app.use('/modules', express.static(__dirname + '/node_modules'));
app.use('/pdb', express.static(__dirname +
'/node_modules/advanced-sheet-handler/dist/collection/pdb'));
// app.use('/pdb', express.static(__dirname + '/data/pdb'));
}
/*
* Connect the server to the client : for front-end tests and normal modes.
* @worker [function] : to run (for front-end tests = a "pipo" function,
* for normal mode = a function performing the computations)
* @downloader [function] : to download files (for front-end tests = same pipo function,
* for normal mode = a function creating the files and returning their path)
* @downloadRoute [string] : path to the cache directory where download files will be saved
*/
var httpStart = function (worker, downloader, downloadRoute, port, dbEndpoints) {
if (! worker) throw 'No worker function detected';
if (! downloader) throw 'No downloader function detected';
if (! downloadRoute) throw 'No downloadRoute function detected';
setClientRoute(app, downloadRoute);
app.get('/', function(req, res) {
res.sendFile(__dirname+'/src/index.html');
});
app.get('/apiWhite/:request/:opt?',function(req, res){
let chunkRes = '';
let chunkError = '';
let url = dbEndpoints.proteins
if (req.params.opt === undefined){
url = url + req.params.request
}
else{
url = url + req.params.request+'/'+req.params.opt
}
let curl = spawn('curl', ['-X', 'GET', url]);
console.log(url)
curl.stdout.on('data', (data) => {
chunkRes += data.toString('utf8');
})
curl.stderr.on('data', (data) => {
chunkError+= data.toString('utf8');
})
curl.on('close', (code) => {
//console.log('--------------')
//console.log(chunkRes)
res.send(chunkRes)
//console.log('--------------')
})
})
app.get('/apiDet/:request?/:opt?',function(req, res){
let chunkRes = '';
let chunkError = '';
let url = dbEndpoints.detergents
if(!req.params.request){
url = url;
}
else{
if (!req.params.opt){
url = url + req.params.request
}
else{
url = url + req.params.request+'/'+req.params.opt
}
}
let curl = spawn('curl', ['-X', 'GET', url]);
console.log(url)
curl.stdout.on('data', (data) => {
chunkRes += data.toString('utf8');
})
curl.stderr.on('data', (data) => {
chunkError+= data.toString('utf8');
})
curl.on('close', (code) => {
//console.log('--------------')
//console.log(chunkRes)
res.send(chunkRes)
//console.log('--------------')
})
})
// listening the server
server.listen(port, function () {
console.log('Server listening on port ' + port + ' !');
});
// connection via socket
let PDB_RESULTS;
io.on("connection", function (socket) {
console.log("io on connection")
socket
.on("submission", function (data) {
worker(data)
.on('jobCompletion', function (jsonRes) {
console.log("jobCompleted")
PDB_RESULTS = jsonRes;
socket.emit('results', PDB_RESULTS);
})
.on('stderrContent', function (err) {
socket.emit('stderrContent');
});
})
.on("downloadPymol", function (newData) {
var mode = "pymolScript";
downloader(mode, newData).on('pymolAvailable', function (pathFile) {
socket.emit('fileAvailable', {"mode" : mode, "path" : pathFile});
});
})
.on("downloadChimera", function (newData) {
var mode = "chimeraScript";
downloader(mode, newData).on('chimeraAvailable', function (pathFile) {
socket.emit('fileAvailable', {"mode" : mode, "path" : pathFile});
});
})
.on("downloadPdb", function (newData) {
console.log("on download pdb")
var mode = 'pdbFile';
downloader(mode, newData, PDB_RESULTS).on('pdbAvailable', function (pathFile) {
socket.emit('fileAvailable', {"mode" : mode, "path" : pathFile});
});
})
.on("downloadData", function (newData) {
var mode = 'dataFile';
downloader(mode, newData).on('dataAvailable', function (pathFile) {
socket.emit('fileAvailable', {"mode" : mode, "path" : pathFile});
});
})
.on("downloadZip", function (newData) {
var mode = 'zip';
downloader(mode, newData, PDB_RESULTS).on('zipAvailable', function (pathFile) {
socket.emit('fileAvailable', {"mode" : mode, "path" : pathFile});
});
});
});
}
module.exports = {
httpStart : httpStart,
backCompute : backCompute
};