Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (72 loc) · 3.18 KB
/
Copy pathserver.js
File metadata and controls
92 lines (72 loc) · 3.18 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const express = require('express')
const fs = require("fs")
const bodyParser = require('body-parser')
const path = require('path')
const https = require('https')
const DataTools = require('./datatools');
const app = express()
// Pour accepter les connexions cross-domain (CORS)
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
next();
});
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const prefix = '/datalens'
app.use(prefix, express.static(path.join(__dirname, 'public')))
// Serve static files from 'node_modules' (optional: use a prefix to avoid conflicts)
app.use(prefix + '/node_modules', express.static(path.join(__dirname, 'node_modules')));
app.get(prefix, (req, res) => {
res.render('about')
})
app.get(prefix + '/explorer', (req, res) => {
res.render('index')
})
app.post(prefix + '/data/:action', async (req, res) => {
let filters = req.body; // expects { modality: [], license: [], tasks: [] }
const folderPath = 'data/modality_datasets';
const dataTools = new DataTools(folderPath);
// console.log(filters)
let filterPath = path.join(__dirname, 'data/filters.json')
let result;
if (req.params.action === 'filters' && fs.existsSync(filterPath)) {
result = fs.readFileSync(filterPath)
} else {
let cachefile = `data/cache/${dataTools.getFilterHash(filters)}.json`
if (fs.existsSync(path.join(__dirname, cachefile))) {
// console.log(`Loading cache file ${cachefile}`)
result = fs.readFileSync(path.join(__dirname, cachefile))
// console.log(`Dataset has ${result.data.length} entries.`)
} else {
await dataTools.init(req.params.action, filters)
result = await dataTools.getData()
try {
result = JSON.stringify(result, null, 4)
} catch(e) {
result = { message: 'The resulting dataset is too large to be displayed. Please review your filters.' }
result = JSON.stringify(result)
}
}
}
res.send(result)
// res.json(result);
});
const port = 8015 // verify the availability of this port on the server
const portHTTPS = 8017
app.listen(port, async () => { console.log(`HTTP Server started at port ${port}.`) })
try {
var privateKey = fs.readFileSync( '/etc/httpd/certificate/exp_20250808/dataviz_i3s_unice_fr.key' );
var certificate = fs.readFileSync( '/etc/httpd/certificate/exp_20250808/dataviz_i3s_unice_fr_cert.crt' );
var ca = fs.readFileSync( '/etc/httpd/certificate/exp_20250808/dataviz_i3s_unice_fr_AC.cer' );
var options = {key: privateKey, cert: certificate, ca: ca};
https.createServer( options, function(req,res)
{
app.handle( req, res );
} ).listen( portHTTPS, async () => { console.log(`HTTPS Server started at port ${portHTTPS}.`) } );
} catch(e) {
console.log("Could not start HTTPS server")
}