forked from OriHoch/socialmap-app-main-page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (66 loc) · 2.33 KB
/
index.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
'use strict';
const path = require('path');
const fs = require('fs');
const express = require('express');
const nunjucks = require('nunjucks');
const request = require("request");
const urlencode = require('urlencode');
const basePath = process.env.BASE_PATH || '/';
const rootPath = path.resolve(__dirname, './dist/socialmap-app-main-page/');
const disableCache = process.env.DISABLE_CACHE || false;
const app = express();
if (disableCache) {
app.disable('view cache');
}
nunjucks.configure(rootPath, {
autoescape: true,
noCache: disableCache,
express: app
});
app.set('port', process.env.PORT || 8000);
app.get(basePath + '*', function(req, res) {
var filePath = path.resolve(rootPath, req.params[0]);
if (fs.existsSync(filePath)) {
var stats = fs.lstatSync(filePath);
if (stats.isFile()) {
return res.sendFile(filePath);
}
}
let doc_id = 'reports/ngos-main-page';
request({
url: 'https://next.obudget.org/get/' + urlencode(doc_id),
json: true
}, function (error, response, body) {
if (response && response.statusCode === 200 && body !== null && body.value) {
var theme = 'socialmap';
var themeFileName = 'theme.'+theme+'.he.json';
var themeScript = '';
var themeJson = null;
// try the themes root directory first - this allows mount multiple themes in a single shared docker volume
if (fs.existsSync(path.resolve('/themes', themeFileName))) {
themeJson = JSON.parse(fs.readFileSync(path.resolve('/themes', themeFileName)));
// fallback to local file - for local development / testing
} else if (fs.existsSync(path.resolve(__dirname, themeFileName))) {
themeJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, themeFileName)));
}
if (themeJson) {
for (var key in themeJson) {
themeScript += key+"="+JSON.stringify(themeJson[key])+";";
}
themeScript += "BUDGETKEY_THEME_ID=" + JSON.stringify(theme) + ";";
}
body = body.value;
res.render('index.html', {
base: basePath,
themeScript: themeScript,
prefetchedData: JSON.stringify(body),
});
} else {
console.log(error);
res.sendStatus(response ? response.statusCode : 500);
}
});
});
app.listen(app.get('port'), function() {
console.log('Listening port ' + app.get('port'));
});