This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
forked from MShahzaib/VoColDoco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·374 lines (340 loc) · 10.9 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var documentation = require('./routes/documentation');
var evolution = require('./routes/evolution');
var startup = require('./routes/startup');
var validation = require('./routes/validation');
var client = require('./routes/clientServices');
var login = require('./routes/login');
var adminLogin = require('./routes/adminLogin');
var referenceRoutes = require('./routes/referenceRoutes');
var listener = require('./routes/listener');
var config = require('./routes/config');
var fs = require('fs');
var jsonfile = require('jsonfile');
var app = express();
var watch = require('node-watch');
var shell = require('shelljs');
var router = express.Router();
var proxy = require('express-http-proxy');
var shell = require('shelljs');
var session = require('express-session');
var escapeHtml = require('escape-html');
var sparqlServer = require('./routes/sparqlServer');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// no caching
app.use(function(req, res, next) {
res.header('Cache-Control',
'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next()
});
app.locals.isExistSyntaxError = "false";
var ErrorsFilePath = __dirname + '/jsonDataFiles/syntaxErrors.json';
function readSyntaxErrorsFile() {
if (fs.existsSync(ErrorsFilePath)) {
var data = fs.readFileSync(ErrorsFilePath);
if (data.toString().includes('1')) {
app.locals.isExistSyntaxError = "true";
} else {
app.locals.isExistSyntaxError = "false";
}
}
}
readSyntaxErrorsFile();
// check if the userConfigurations file is exist
// for the first time of app running
var userConfigurationsFile = __dirname +
'/jsonDataFiles/userConfigurations.json';
var repositoryURL = "";
app.locals.projectTitle = "MobiVoc";
app.locals.userConfigurations = Array(6).fill(true);
app.locals.isExistAdminAccount = false;
app.locals.repositoryURL = "";
app.locals.repoParam = Array(3).fill("");
function readUserConfigurationFile() {
if (fs.existsSync(userConfigurationsFile)) {
var data = fs.readFileSync(userConfigurationsFile);
if (data.includes('vocabularyName')) {
jsonfile.readFile(userConfigurationsFile, function(err, obj) {
var menu = Array(7).fill(false);
var repoParam = Array(3).fill("");
var loginUserName = "";
var adminAccount = "";
Object.keys(obj).forEach(function(k) {
if (k === "vocabularyName") {
// store projectTitle to be used by header.ejs
app.locals.projectTitle = obj[k];
} else if (k === "repositoryURL") {
// store repositoryURL to be checked if it was uploaded
// for first time or was changed to another repository
repositoryURL = obj[k];
app.locals.repositoryURL = obj[k];
} else if (k === "repositoryOwner") { //repoParam[0]
repoParam[0] = obj[k];
} else if (k === "repositoryName") { //repoParam[1]
repoParam[1] = obj[k];
} else if (k === "branchName") { //repoParam[2]
repoParam[2] = obj[k];
} else if (k === "turtleEditor") { //menu[0]
menu[0] = true;
} else if (k === "documentationGeneration") { //menu[1]
menu[1] = true;
} else if (k === "visualization") { //menu[2]
menu[2] = true;
} else if (k === "sparqlEndPoint") { //menu[3]
menu[3] = true;
} else if (k === "evolutionReport") { //menu[4]
menu[4] = true;
} else if (k === "analytics") { //menu[5]
menu[5] = true;
} else if (k === "infomationProtectionAgreement") {
if (obj['text2'] != "") {
var dataProtectionHtmlPage = '<% include header.ejs %><div style="margin-top: 3% !important;"></div><div class="ui grid"><div class="ui container">'
dataProtectionHtmlPage += obj['text2'];
dataProtectionHtmlPage += '</div></div><% include footer .ejs%>';
fs.writeFileSync(__dirname + '/views/dataProtection.ejs',
dataProtectionHtmlPage, {
encoding: 'utf8',
flag: 'w'
});
}
if (obj['text3'] != "") {
fs.writeFileSync(__dirname +
'/views/dataProtectionScript.ejs', obj['text3'], {
encoding: 'utf8',
flag: 'w'
});
}
menu[6] = true;
} else if (k === "loginUserName") {
loginUserName = obj[k];
} else if (k === "adminUserName") {
adminAccount = obj[k];
}
});
app.locals.userConfigurations = menu;
app.locals.repoParam = repoParam;
// check if the admin select private mode access of instace
if (loginUserName)
app.locals.authRequired = true;
else
app.locals.authRequired = false;
// save local variable about existing of admin account
if (adminAccount)
app.locals.isExistAdminAccount = true;
else
app.locals.isExistAdminAccount = false;
});
}
}
}
readUserConfigurationFile();
// check if the user has an error and this was for first time or
// when user has changed to another repositoryURL
// currentrepositoryURL === "" means it is the first time
var currentrepositoryURL = "";
var repoFolderPath = "../repoFolder";
if (fs.existsSync(repoFolderPath)) {
currentrepositoryURL = shell.exec('git ls-remote --get-url', {
silent: true
}).stdout;
shell.cd('../vocol', {
silent: false
}).stdout;
}
app.use(session({
secret: 'secretC44-4D44-WppQ38S',
resave: true,
saveUninitialized: true,
cookie: {
path: '/',
maxAge: 10 * 30 * 1000
}
}));
// redirect to /config for first time if userConfigurations.json does not exsit
app.get('*', function(req, res, next) {
var userConfigurationsFile2 = __dirname +
'/jsonDataFiles/userConfigurations.json';
var data = fs.readFileSync(userConfigurationsFile2, "utf8");
if (!data.includes("adminUserName"))
res.render('config', {
title: 'Configuration Page',
inputComponentsValues: "",
data: {},
errors: {}
});
else {
next();
}
});
// routing to the available routes on the app
app.use(['\/\/', '/'], routes);
app.use(['\/\/documentation', '/documentation'], documentation);
app.use(['\/\/webvowlLink', '/webvowlLink'], express.static(path.join(__dirname,
"views/webvowl")));
app.use(['\/\/turtleEditorLink', '/turtleEditorLink'], express.static(path.join(
__dirname, "views/editor")));
app.use(['\/\/analyticsLink', '/analyticsLink'], express.static(path.join(
__dirname, "views/d3sparql")));
app.use(['\/\/evolution', '/evolution'], evolution);
app.use(['\/\/startup', '/startup'], startup);
app.use(['\/\/validation', '/validation'], validation);
app.use(['\/\/client', '/client'], client);
app.use(['\/\/listener', '/listener'], listener);
app.use(['\/\/login', '/login'], login);
app.use(['\/\/adminLogin', '/adminLogin'], adminLogin);
app.use(['\/\/config', '/config'], config);
app.use(['\/\/sparqlServer', '/sparqlServer'], sparqlServer);
app.use(['\/\/fuseki/', '/fuseki/'], proxy('localhost:' + process.argv.slice(2)[
1] || 3030 + '/', {
proxyReqPathResolver: function(req) {
if (req.method === 'POST')
return require('url').parse(req.url).path + "?query=" + escape(
req.body.query);
else
return require('url').parse(req.url).path;
}
}));
app.get(['\/\/analytics', '/analytics'], function(req, res) {
if (!req.session.isAuthenticated && req.app.locals.authRequired)
res.render('login', {
title: 'login',
hash: ""
});
else
res.render('analytics', {
title: 'Analytics'
});
})
app.get(['\/\/editor', '/editor'], function(req, res) {
if (!req.session.isAuthenticated && req.app.locals.authRequired)
res.render('login', {
title: 'login',
hash: ""
});
else
res.render('editor', {
title: 'Editing'
});
})
app.get(['\/\/visualization', '/visualization'], function(req, res) {
if (!req.session.isAuthenticated && req.app.locals.authRequired)
res.render('login', {
title: 'login',
hash: ""
});
else
res.render('visualization', {
title: 'Visualization'
});
})
app.get(['\/\/querying', '/querying'], function(req, res) {
if (!req.session.isAuthenticated && req.app.locals.authRequired)
res.render('login', {
title: 'login',
hash: ""
});
else
res.render('querying', {
title: 'Querying'
});
});
app.get(['\/\/Imprint', '/Imprint'], function(req, res) {
if (!req.session.isAuthenticated && req.app.locals.authRequired)
res.render('login', {
title: 'login',
hash: ""
});
else
res.render('Imprint', {
title: 'Imprint'
});
});
app.get(['\/\/dataProtection', '/dataProtection'], function(req, res) {
if (!req.session.isAuthenticated && req.app.locals.authRequired)
res.render('login', {
title: 'login',
hash: ""
});
else
res.render('dataProtection', {
title: 'dataProtection'
});
});
app.get(['\/\/checkErrors', '/checkErrors'], function(req, res, next) {
readSyntaxErrorsFile();
res.send(app.locals.isExistSyntaxError);
res.end();
});
app.get(['\/\/getRepoInfo', '/getRepoInfo'], function(req, res, next) {
readUserConfigurationFile();
res.send(app.locals.repoParam);
res.end();
});
// catch something else
app.use(['//*', '*'], referenceRoutes);
// monitor ErrorsFilePath
watch(ErrorsFilePath, {
recursive: true
}, function(evt, name) {
if (evt == 'update') {
// call if SyntaxErrors file was changed
readSyntaxErrorsFile();
}
});
// monitor change of userConfigurationsFile
watch(userConfigurationsFile, {
recursive: true
}, function(evt, name) {
if (evt == 'update') {
// call if userConfigurations file was changed
readUserConfigurationFile();
}
});
function isEmptyObject(obj) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
}
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err,
title: 'error'
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {},
title: 'error'
});
});
module.exports = app;