-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpnt-downloader-server.js
More file actions
70 lines (60 loc) · 1.97 KB
/
pnt-downloader-server.js
File metadata and controls
70 lines (60 loc) · 1.97 KB
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
const dataRequester = require("./pnt-data-requester");
const http = require("http");
const host = 'localhost';
const port = process.env.PORT || 16132;
const requestListener = async function (req, res) {
res.setHeader("Content-Type", "application/json");
try {
let url = new URL(req.url, "http://localhost");
// console.log(url.searchParams);
switch (url.pathname) {
case "/requestData":
result = await requestData();
res.writeHead(200);
res.end(JSON.stringify(result));
break
case "/downloadFile":
console.log('--- INITIATING FILE DOWNLOAD REQUEST ---');
result = await downloadFile(url.searchParams.get("src"),url.searchParams.get("dest"),url.searchParams.get("filename"),url.searchParams.get("datadir"));
if(result.status == 'completed')
res.writeHead(200);
else
res.writeHead(555);
res.end(JSON.stringify(result));
break
default:
res.writeHead(404);
res.end(`{"error": "Not found"}`);
}
}
catch(e) {
console.log("PDS: error in controller",e);
res.writeHead(500);
res.end(`{"error": ${JSON.stringify(e)}}`);
}
return false;
};
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
function requestData() {
console.log("Request Data");
try {
return dataRequester.request_pnt_data();
}
catch(e) {
return e;
}
}
function downloadFile(src,dest,filename,datadir) {
console.log("downloadFile",src,dest,filename,datadir);
try {
// return 1
return dataRequester.download_file(src,dest,filename,datadir);
// return dataRequester.request_pnt_data();
}
catch(e) {
return e;
}
}