/
index.js
169 lines (148 loc) · 4.66 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
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
/* eslint-env es6 */
'use strict';
const getWebAppData = require('./get-web-app-data');
Error.stackTraceLimit = Infinity;
const express = require('express');
const exphbs = require('express-handlebars');
const helmet = require('helmet');
const csp = require('helmet-csp');
const color = require('color');
const bluebird = require('bluebird');
const redis = require('redis');
const REDIS_INDEX_KEY = "INDEX_V4";
const REDIS_ENTRY_NAMESPACE = "ENTRY_V4_";
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
const client = process.env.REDIS_URL && redis.createClient({
url: process.env.REDIS_URL
});
const app = express();
app.set('json spaces', 2);
app.use(helmet());
app.use(csp({
// Specify directives as normal.
directives: {
defaultSrc: ['\'self\'', 'http:', 'https:'],
scriptSrc: ['\'self\'', 'cdn.polyfill.io', 'platform.twitter.com', 'https://ssl.google-analytics.com/ga.js'],
styleSrc: ['\'self\'', 'fonts.googleapis.com', '\'unsafe-inline\''],
fontSrc: ['\'self\'', 'fonts.gstatic.com'],
imgSrc: ['\'self\'', 'data:', 'https:'],
// reportUri: '/api/report-violation',
frameAncestors: ['none'],
objectSrc: [], // An empty array allows nothing through
},
// Set to true if you want to set all headers: Content-Security-Policy,
// X-WebKit-CSP, and X-Content-Security-Policy.
setAllHeaders: true,
// Set to true if you want to disable CSP on Android where it can be buggy.
disableAndroid: false,
// Set to false if you want to disable any user-agent sniffing.
// This may make the headers less compatible but it will be much faster.
// This defaults to `true`. Should be false if behind cdn.
browserSniff: false
}));
// Use Handlebars for templating
const hbs = exphbs.create({
helpers: {
ifEq: function(a, b, options) {
return (a === b) ? options.fn(this) : options.inverse(this);
},
mangle: function(options) {
return options.fn(this).replace(/[^a-z0-9]+/ig, '-');
},
bytesToMegabytes: function(options) {
return (Number(options.fn(this)) / (1024 * 1024)).toFixed(2) + 'MB';
},
encodeURIComponent: function(options) {
return encodeURIComponent(options.fn(this));
},
fontColor: function(options) {
return color(options.fn(this) || '#ffffff').light() ? 'black' : 'white';
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use('/static', express.static(__dirname + '/static', {
maxAge: 3600 * 1000 * 24
}));
app.use('/sw-toolbox', express.static(__dirname + '/node_modules/sw-toolbox/', {
maxAge: 3600 * 1000 * 24
}));
app.use('/sw.js', express.static(__dirname + '/static/scripts/sw.js', {
maxAge: 0
}));
app.get('/', function (req, res) {
client.smembersAsync(REDIS_INDEX_KEY).then(data => {
const toFetch = data.map(a => client.getAsync(a));
return Promise.all(toFetch)
.then(items => items
.map(j => JSON.parse(j))
.filter(i => !!i)
.sort((a, b) => b.timestamp - a.timestamp)
.slice(0, 10))
.then(items => res.render('index', { items }));
});
});
app.get('/add', function (req, res) {
client.smembersAsync(REDIS_INDEX_KEY).then(data => {
const toFetch = data.map(a => client.getAsync(a));
return Promise.all(toFetch)
.then(items => items
.map(j => JSON.parse(j))
.filter(i => !!i)
.sort((a, b) => b.timestamp - a.timestamp)
.slice(0, 10))
.then(items => res.render('index', { items, add: true }));
});
});
app.get('/save/', function (req, res) {
if (!req.query.url) {
res.status(500);
res.end('No url param!');
return;
}
if (!req.query.url) {
res.status(500);
res.end('No category param!');
return;
}
getWebAppData(req.query.url)
.then(data => {
data.category = (req.query.category && (req.query.category.constructor === Array ? req.query.category : [req.query.category])) || ['uncategorised'];
data.timestamp = Date.now();
const key = REDIS_ENTRY_NAMESPACE + req.query.url;
return client.setAsync(key, JSON.stringify(data))
.then(() => client.expireAsync(key, 3600 * 24 * 30))
.then(() => client.saddAsync(REDIS_INDEX_KEY, key))
.then(() => client.scardAsync(REDIS_INDEX_KEY).then(no => console.log(no)))
.then(() => res.redirect('/'));
})
.catch(e => {
res.status(500);
res.end(e.message);
console.log(e);
});
})
app.get('/get-app-data', function(req, res) {
if (!req.query.url) {
res.status(500);
res.end('No url param!');
return;
}
getWebAppData(req.query.url)
.then(data => {
res.render('index', {
items: [data],
confirm: true,
url: req.query.url
});
})
.catch(e => {
res.status(500);
res.end(e.message);
console.log(e);
});
});
app.listen(process.env.PORT || 3000);
console.log('Listening on', process.env.PORT || 3000);