-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
133 lines (113 loc) · 4.02 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
/* eslint-disable no-console */
const es = require('event-stream');
const JSONStream = require('JSONStream');
const sha256 = require('sha256');
const debug = require('debug')('bibliomap-viewer');
const net = require('net');
const express = require('express');
const app = express();
const httpServer = require('http').Server(app);
const io = require('socket.io')(httpServer);
const fs = require('fs');
const useragent = require('useragent');
const config = require('./config.js');
const pkg = require('./package.json');
const enricherCfg = config.listen['bibliomap-enricher'];
// list of connected websockets
const websockets = {};
/**
* bibliomap-enricher => bibliomap-viewer
*/
const server = net.createServer((socket) => { // 'connection' listener
console.log('bibliomap-enricher connected');
socket.on('end', () => {
console.log('bibliomap-enricher disconnected');
});
// get and parse the bibliomap-enricher JSON stream
// then send it to the browser
socket
.pipe(JSONStream.parse())
.pipe(es.mapSync((event) => {
Object.keys(websockets).forEach((sid) => {
debug('ezPAARSE EC recevied: ', event);
// We don't need EC's that are not geolocalized
if (!event['geoip-latitude'] || !event['geoip-longitude']) { return; }
// Approximate coordinates in a radius of ~20km (0.2°)
event['geoip-latitude'] = parseFloat(event['geoip-latitude']) + 0.4 * (Math.random() - 0.5);
event['geoip-longitude'] = parseFloat(event['geoip-longitude']) + 0.4 * (Math.random() - 0.5);
if (event.host) {
event.host = sha256(event.host);
}
// send the filtered EC to the client through websocket
websockets[sid].emit('ezpaarse-ec', event);
});
}));
});
server.listen(enricherCfg.port, enricherCfg.host, () => {
console.log(`Waiting for bibliomap-enricher data at ${enricherCfg.host} : ${enricherCfg.port}`);
});
/**
* bibliomap => web browser through websocket
*/
httpServer.listen(config.listen['bibliomap-viewer'].port, config.listen['bibliomap-viewer'].host);
const entity = process.env.BBV_INDEX || 'cnrs';
app.set('views', `${__dirname}/themes`);
app.use(express.static(__dirname));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', 'GET');
next();
});
app.use((req, res, next) => {
const agent = useragent.is(req.headers['user-agent']).ie;
if (agent) {
return res.render('app/browser-compatibility.html.twig', { entity });
}
next();
return null;
});
app.get('/', (req, res) => {
let locale = req.query.lang || 'fr';
let i18nGlobal;
try {
i18nGlobal = fs.readFileSync(`${__dirname}/themes/app/locale/${locale}.json`, 'utf-8');
} catch (e) {
locale = 'fr';
i18nGlobal = fs.readFileSync(`${__dirname}/themes/app/locale/fr.json`, 'utf-8');
}
i18nGlobal = JSON.parse(i18nGlobal);
i18nGlobal.locale = locale;
const host = `${req.protocol}://${req.get('x-forwarded-host') || req.hostname}`;
res.header('X-UA-Compatible', 'IE=edge');
if (req.query && req.query.lang) {
delete req.query.lang;
}
const query = Object.keys(req.query).map(q => `${q}=${req.query[q]}`).join('&');
return res.render('app/layout.html.twig', {
entity,
version: pkg.version,
i18n: i18nGlobal,
host,
query,
});
});
io.on('connection', (client) => {
console.log(`Web browser connected through websocket ${client.id}`);
websockets[client.id] = client;
client.on('disconnect', () => {
console.log(`Web browser disconnected from the websocket ${client.id}`);
delete websockets[client.id];
});
});
console.log(`Bibliomap is listening on http://${config.listen['bibliomap-viewer'].host}
: ${config.listen['bibliomap-viewer'].port} (open it with your browser!)`);
// exit on CTRL+C
function exitOnSignal(signal) {
process.on(signal, () => {
console.log(`Caught ${signal}, exiting`);
process.exit(1);
});
}
exitOnSignal('SIGINT');
exitOnSignal('SIGTERM');