-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
238 lines (202 loc) · 6.88 KB
/
server.mjs
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
//var fs = require('fs');
//var http = require('http');
//var https = require('https');
//var exec = require("child_process");
//var favicon = require('serve-favicon');
//var util = require("util");
//var uuidEngine = require("uuid");
//var open = require("open");
//const execProm = util.promisify(exec.exec);
//const mkdirProm = util.promisify(fs.mkdir);
import { join,dirname } from 'path';
import {fileURLToPath} from 'url';
import express from 'express';
import cors from 'cors';
import * as http from 'http';
import * as fs from 'fs';
//import { fstat } from 'fs';
//import { ChildProcess } from 'child_process';
//import serveFavicon from 'serve-favicon';
var app = express().use("*",cors());
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
///////////////////////////////////////////////////////////////////////////////
//using letsencrypt
//const options = {
// key: fs.readFileSync('/etc/letsencrypt/live/www.hippocampusanalytics.com/privkey.pem'),
// cert: fs.readFileSync('/etc/letsencrypt/live/www.hippocampusanalytics.com/fullchain.pem')
//};
//var port = (process.env.PORT || process.env.VCAP_APP_PORT || 3000);
app.enable('trust proxy');
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
app.use(express.json( {limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
app.set('/scripts', join(__dirname + '/scripts'));
app.set('/styles', join(__dirname + '/styles'));
app.set('/images', join(__dirname + '/images'));
app.set('/graphs', join(__dirname + '/graphs'));
app.set('/plotdata', join(__dirname + '/plotdata'));
app.get('/', function (req, res) {
res.statusCode = 302;
res.sendFile(join(__dirname, '/public/jargon.html'));
});
app.get('/plot', function (req, res) {
res.statusCode = 200;
res.sendFile(join(__dirname, '/public/plot.html'));
});
app.get('/plotdata', function (req, res) {
res.statusCode = 200;
res.sendFile(join(__dirname, '/plotdata/timeseries.json'));
});
app.get('/data', function (req, res) {
res.statusCode = 200;
res.sendFile(join(__dirname, '/graphs/hello-world.json'));
console.log('data file sent...');
});
app.get('/data/get1', function (req, res) {
res.statusCode = 200;
res.sendFile(join(__dirname, '/graphs/hello-world.json'));
console.log('data file sent...');
});
app.get('/data/get2', function (req, res) {
res.statusCode = 200;
res.sendFile(join(__dirname, '/graphs/demo1.json'));
console.log('data file sent...');
});
app.get('/data/get3', function (req, res) {
res.statusCode = 200;
res.sendFile(join(__dirname, '/graphs/demo2.json'));
console.log('data file sent...');
});
app.get('/data/get4', function (req, res) {
res.statusCode = 200;
try {
if (fs.existsSync(join(__dirname, '/graphs/empty.json'))) {
res.sendFile(join(__dirname, '/graphs/empty.json'));
console.log('data file sent...');
}
} catch(err) {
console.error("empty.json does not exist")
}
});
app.get(`/getlist`, function (req, res) {
res.statusCode = 200;
try {
let files = fs.readdirSync(join(__dirname, '/graphs'));
res.send({ data: files.join(',')})
console.log('files: ', files);
} catch(err) {
console.error("error reading dir")
}
});
app.get(`/getdataFile`, function (req, res) {
res.statusCode = 200;
try {
const file = join(req.query.data + ".json");
const fp = join(__dirname, "/graphs/" + file);
console.log("reading file " + file + " ...")
if (fs.existsSync(fp)) {
res.sendFile(fp);
console.log('data file sent...');
}else{
console.log('cannot read file...');
res.send({ data: 'error 1' })
}
} catch(err) {
console.error("error ")
res.send({ data: err })
}
});
app.get('/getYAMLdataFile', function (req, res) {
res.statusCode = 200;
try {
const file = join(req.query.data + ".yaml");
const fp = join(__dirname, "compiledGraphs/" + file);
console.log("reading file " + file + " ...")
if (fs.existsSync(path.join(dir, file))) {
res.sendFile( path.join(dir, file) );
console.log('data file sent...');
}else{
console.log('cannot read file : ' + path.join(dir,file));
res.send({ stderr: 'cannot read YAML file' })
}
} catch(err) {
console.error("error ", err)
res.send({ data: err })
}
});
app.post('/updateData', function (req, res) {
res.statusCode = 200;
console.log(req.body);
const data = JSON.stringify(req.body, null, "\t");
let graphname = req.body.metadata.name;
let fn = join(__dirname, '/graphs/' + graphname + '.json')
fs.writeFile(fn, data , function (err) {
if (err) throw err;
const resdata = graphname + ' saved... (' + data.length + ' bytes)';
res.send({ data: resdata })
console.log(resdata);
});
});
app.post('/updateDataYAML', function (req, res) {
res.statusCode = 200;
const data = JSON.stringify(req.body, null, "\t");
let graphname = req.body.metadata.name;
console.log("updating file " + graphname + " ...")
let fn = join(__dirname, '/compiledGraphs/' + graphname + '.bak.yaml')
fs.writeFile(fn, data , function (err) {
if (err) throw err;
const resdata = graphname + ' saved... (' + data.length + ' bytes)';
res.send({ data: resdata })
console.log(resdata);
});
});
app.post('/compileData', function (req, res) {
res.statusCode = 200;
console.log(req.body);
const data = JSON.stringify(req.body, null, "\t");
let graphname = req.body.metadata.name;
const {body, statusCode} = got.post('http://jargoncompiler:8080', data);
if (statusCode !== 200 || body.error) {
throw new Error(body.error || 'Oops. Something went wrong! Try again please.');
};
});
app.post('/compiledData', function (req, res) {
res.statusCode = 200;
console.log(req.body);
const data = JSON.stringify(req.body, null, "\t");
let graphname = req.body.metadata.name;
let fn = join(__dirname, '/yamls/' + graphname + '.yaml')
fs.promises
.writeFile(fn, data, { encoding: 'utf8' }, function (err) {
if (err) throw err;
const resdata = graphname + ' (' + data.length + ' bytes)';
res.send({ data: resdata })
// console.log(resdata);
})
});
function startServer(server, port, host) {
function onError(error) {
server
.removeListener('error', onError)
.removeListener('listening', onSuccess)
if (error.code === 'EADDRINUSE') {
startServer(server, ++port, host)
}
}
function onSuccess() {
console.log(
`listening at: (host = ${host}, port = ${server.address().port})`
)
}
server.listen(port, host)
.on('error', onError)
.on('listening', onSuccess)
}
var port = 8080;
const host = '0.0.0.0';
const server = http.createServer(app)
console.log("The server is now listening to port " + port)
startServer(server, port, host)
//var httpServer = http.createServer(app).listen(port);